| ||||||
| ||||||

.NET OverviewGoals:
OverviewMicrosoft .NET is a comprehensive set of technologies for the development, deployment, execution, and integration of software on a wide variety of devices. It contains programming languages, compilers, an execution engine, support for graphical user interface development, support for web service creation, libraries for web site development, etc. As a software developer, it is useful to take a look at the big picture of .NET before focusing in on the area most applicable to your own work. PlatformThe term "platform" comes up regulary in conversations about software development where it typically means "the software layer upon which developers build their applications." Traditionally, the platform is the operating system. The OS is the platform because it supplies libraries of functions that developers leverage in their own code. For example, most operating systems provide the ability to create new processes, create and manipulate threads, access disk files, make network connections, etc. Each application requires some set of these services in order to run.
Writing software directly against the operating system is problematic since each OS offers a slightly different set of services. This is true even within the Windows family of operating systems where Windows 95 does not offer programmers the same platform as Windows XP. Even more annoying is the fact that even widely supported services work differently across the various systems. In addition, operating systems typically provide only relatively low level services which forces programmers to implement complicated ideas such as versioning and security on their own. All these issues make operating system independent code quit difficult to write.
In recent years, the software community has moved away from coding directly against the operating system and has instead adopted "abstract platforms" such as the one offered by Microsoft's .NET initiative. The .NET platform insulates programmers from the operating system. It offers the same set of services everywhere. The services work in exactly the same way everywhere the platform is supported. In addition, .NET offers sophisticated services not traditionally offered by an operating system such as support for security, versioning, remote method invocation, XML web services, etc.
Microsoft implements .NET for the various flavors of Windows. In addition, Microsoft has given ownership of large portions of .NET to the standards body ECMA. This means developers can implement .NET to run on operating systems other than Windows. .NET PiecesThe phrase ".NET" is a marketing term that has been applied to an entire family of Microsoft products. The pieces can be divided into four categories: development, platform, servers, services.
The development category contains the new .NET programming languages and the development tools such as compilers and the Visual Studio 2010 development environment. The platform is made up of the libraries and the runtime. The library is officially called the ".NET Framework Class Library" and includes all the fancy services programmers enjoy: treading, remoting, disk access, string handling, etc. The execution engine is called the "Common Language Runtime" and is responsible for managing the execution of a .NET program. The .NET Enterprise Servers cover a wide range of server side development and processing needs. For example, there are servers for data storage (SQL Server), collaboration (SharePoint Portal Server and Exchange Server), website management (Application Center), application integration (BizTalk Server), e-commerce (Commerce Server), etc. Microsoft has built a few value added services such as .NET Passport and .NET Alerts. Passport manages personal information and facilitates website login. Alerts offer push technology to send messages to interested parties via e-mail, Messenger, etc. .NET LanguagesThere are many programming languages which can be used to write .NET applications. The two most publicized are C# and VB.NET; however, many other languages are available as well. When choosing a language the basic story is simple: if you know VB6 then VB.NET will be most comfortable for you; however, if you come from the C, C++, or Java worlds then C# is the natural choice. In practice, the decision is often not quite so clear cut since other issues can be important. For example, language features can be a factor (e.g. C# offers unsigned numeric types while VB.NET does not), as can corporate mandate (desire to standardize the entire organization on a single language). While the various .NET language differ in many details, they all are offer the same core set of features; for example, they all support inheritance and provide ways to handle exceptions. There is a formal document called the "Common Language Specification (CLS)" that describes the required language features. Every .NET language must support all the things defined in the CLS. Note that languages are allowed to supply additional features beyond the required core if they would like to. Language choice in .NET is less important than in other systems for two reasons. First, all .NET languages have access to the services offered by the standard libraries. Second, all the languages interoperate seamlessly. For example, C# code can call functions written in VB.NET and vice versa. The only caveat is that for cross language interoperation, programmers should restrict themselves to CLS compliant language features. In other words, if a programmers takes advantage of a C# feature such as an unsigned integer, that code will not interoperate with VB.NET since VB.NET does not offer unsigned types. .NET LibraryThe official name for the standard library is the ".NET Framework Class Library." The library is large and complicated; in fact, many people say that learning a .NET language goes relatively quickly and the majority of study time is spent exploring the library. Even though the library is large, it is well organized and easy to navigate. There are some pieces that will be relevant to almost all programs; for example, string handling, collections, and basic input/output fall into this category. Then there are features that will be needed by only more sophisticated applications; for example threading and networking. Finally there are large portions dedicated to particular application targets; for example, website creation, GUI building, XML web service publishing, etc.
Compilation and ExecutionThe term "compile" traditionally means "translate from source code to machine code." That is, the output of the compiler has usually been low level machine code that is completely ready to run on the target machine. The .NET world uses a different model: a .NET compiler translates source code into Common Intermediate Language (CIL or IL for short).
IL is much closer to the machine than the original C# source, yet it is higher level than traditional assembler and remains processor independent.
This compilation model has some important consequences. One immediate advantage is that it enables all .NET languages to easily interoperate since they all compile to the same intermediate format. A more interesting point is that a second translation step is now required in order to execute a .NET program since the IL must be converted to machine code. The translation from IL to machine code is handled by the Common Language Runtime (CLR). The CLR is responsible for managing all aspects of the execution of a .NET application. At runtime, the CLR loads the IL produced by the compiler, translates the IL into machine code, and executes the resulting machine code. Because of this runtime model, .NET applications are often called "managed applications" since the CLR manages their execution.
The CLR translates IL into machine code in a reasonably intelligent way. The first time a method is called, the IL for the method is translated using the runtime compiler called the "just-in-time" compiler (JIT). The machine code for the method is then cached in memory and then the method is executed. The next time that same method is called, the CLR uses the compiled version in the cache and does not recompile the IL.
The entire process goes something like this. When a .NET application is started, the operating system detects that the CLR is required for the application to execute and the CLR is loaded and given control. At this point, all methods are in IL and the cache of machine code is empty. Each method that gets called is JIT compiled and stored in cache. As the program runs, more and more methods are JIT compiled and stored in cache. Of course, only the methods that get called are compiled; no time is wasted compiling unused code paths. When the program exits, the CLR shuts down, the cache is destroyed and all the compiled machine code is erased. Everything starts over from the beginning the next time the program is run. The CLR is highly optimized to make the entire process as efficient as possible.
In order for a .NET program to run, the CLR must be installed on the execution machine.
There are several options for getting the CLR installed. First, it will be included in
Windows releases and/or service packs so many end users may already have it installed.
Second, Microsoft makes available C# Program
A simple C# program which displays a string on the console is shown below. C# follows the
C language family tradition of concise syntax; for example, instead of
C# source code is placed in a disk file with a
A
A
The
To display output on the console, the library method Application TargetsThere are three primary application targets available to the C# developer: console application, Windows application, and class library.
A console application in an executable program. The file containing the code will have
a
A Windows application is an executable program. The file containing the code will have
a
A class library is simply a group of types that can be used by other developers in
their programs. The library is usually contained in a file with a Command Line BuildIt is possible to develop and run .NET applications using command line tools. Notepad can be used as a text editor, the C# compiler can be invoked from a Command Prompt, and the CLR is automatically started when the executable is run. The necessary pieces: compiler, CLR, and .NET Framework Class Library are bundled into the .NET Framework SDK which is available from Microsoft at no cost.
The C# compiler is named
To perform a command line build, the compiler needs to know a fairly standard set of
details: the type of application to build, the output file name, and the source file(s).
The
Specifying a target type of
Default switch values can sometimes simplify the command line. The default target
type is console executable. The default output file name is based on the name
of the source file containing the
A target of
A target of
Visual Studio 2010Microsoft's Visual Studio 2010 is the graphic development environment for .NET programming. It is not part of the free SDK and must be purchased. Visual Studio 2010 includes an editor, debugger, compiler, etc. along with a nice user interface for accessing all the features. Visual Studio 2010 greatly simplifies C# development. Instead of having to remember command line switches, programmers create a "project" that corresponds to their application type and the environment automatically passes the appropriate switches to the compiler. Visual Studio .NET offers a wide variety of project types but it's still easy to identify the three basic C# project types: "Console Application", "Windows Application", and "Class Library." This material is excerpted from the Programming C# course offered by DevelopMentor. |