google ads

Sunday, May 31, 2009

SIMPLE APPLICATION

In this section we look at the proverbial “Hello World” program. For the
purposes of comparison, source code for all three .NET languages
(VB.NET, C#, and managed C++) is given below. Readers might want
to consult Chapter 2 to install the .NET Framework before proceeding.

VB.NET Application
Visual Basic developers are reminded that VB.NET now includes a
command-line compiler (VBC.EXE), allowing one to develop applications
outside the Visual Basic environment. In other words, you can
write the following program in a text editor such as Notepad. VB users
will also see from this example that VB.NET has the ability to produce
console applications, something previous versions of Visual Basic were
unable to do.
'VB.NET "Hello World" Program.
Module HelloWorld
Sub Main
'Use the .NET Runtime Console method WriteLine,
'to output "Hello World" on the screen:
System.Console.WriteLine("Hello World!")
End Sub
End Module
Listing 1.1 VB.NET Hello World program

C# Application
As with the Visual Basic example, you can write this code using any text
editor. Notice that the syntax is very similar to C++ in that class definitions
and methods are encapsulated within curly braces and individual
lines of code end with semicolons.
// C# "Hello World" Program.
public class HelloWorld {
static public void Main () {
System.Console.WriteLine("Hello World!");
}
}
Listing 1.2 C# Hello World program

Managed C++ Application
Managed C++ is almost identical to normal C++. Notice that you must
use the a::b notation for namespaces, rather than the a.b notation of Visual
Basic and C#.
// Managed C++ "Hello World" Program.
// Reference the .NET Runtime Library,
// for Console Input/Output functionality.

#using
void main() {
System::Console::WriteLine("Hello World!");
}
Listing 1.3 Managed C++ Hello World program

Compiling and Running the Example
Assuming that these files were called Hello-World.vb, Hello-World.cs,
and Hello-World.cpp, respectively, .NET console applications could be
An Introduction to the .NET Framework . 9
created by invoking each language’s compiler from the command line as
shown below (alternatively, you could create VB.NET, C#, and C++
console projects and compile them from the VS.NET IDE).
• VB.NET: vbc.exe /t:exe Hello-World.vb
• C#: csc.exe /t:exe Hello-World.cs
• Managed C++: cl.exe /CLR Hello-World.cpp
The /t:exe option informs both the VB.NET and C# compilers to produce
executable files, while the /CLR switch instructs the Visual C++
compiler to produce IL code (this option is OFF by default).

Source Analysis
The most notable difference between the three programs is that the managed
C++ example must explicitly reference the .NET Runtime classes,
which is implicitly done by the VB and C# compilers. This is accomplished
by inserting the following line at the top of all managed
C++ programs: #using . C++ COM/ATL developers
will find this command very similar to the #import directive used in Visual
C++.
Syntactical differences aside, the three programs are remarkably
similar in that they all use the Runtime Console method WriteLine() to
print “Hello World” on the screen. Such uniformity is a virtue of the
.NET Runtime—all three languages use a consistent set of classes to accomplish
the same thing. The only difference lies in the way such
classes are accessed. C++ users might recognize that we had to tell the
compiler which namespace the Console class could be found in. The
concept of namespaces and their importance to the Runtime is addressed
in the .NET Runtime section of this chapter.
Topic: The Common Language Runtime
At the heart of the .NET Framework is the Common Language Runtime
(CLR). In addition to acting as a virtual machine, interpreting and executing
IL code on the fly, the CLR performs numerous other functions,
such as type safety checking, application memory isolation, memory
management, garbage collection, and crosslanguage exception handling.

THE COMMON TYPE SYSTEM
The CLR greatly simplifies crosslanguage communication through the
introduction of the Common Type System (CTS). The CTS defines all of
the basic types that can be used in the .NET Framework and the operations
that can be performed on those types. Applications can create more
complex types, but they must be built from the types defined by the
CTS.
All CTS types are classes that derive from a base class called System.
Object (this is true even for “primitive” types such as integer and
floating point variables). This means that any object executing inside the
CLR can utilize the member functions of the System.Object class. The
methods of System.Object can be found at aNET010005, but for
the purposes of illustration we will consider the Equals() method of this
class, which can be used to test two objects for equality.
Consider the following straightforward C# fragment:
int a=5;
int b=5;
if (a==b) {
System.Console.WriteLine("a is the same as b");
}
Since all types inherit from System.Object, the code could be rewritten
as:
int a=5;
int b=5;
if (a.Equals(b)) {
System.Console.WriteLine("a is the same as b");
}

No comments:

Post a Comment