C# is the hot new object-oriented language for Microsoft’s .NET. So before
we start talking about the language itself, it would be appropriate to get an
overview of Microsoft.NET and the .NET Framework which includes Common Language
Runtime (CLR) and the Framework class libraries.
.NET overview
So what exactly is .NET?
Microsoft.NET is Microsoft’s strategy for delivering software as a service.
The key parts of .NET are as follows —
Microsoft.NET platform
This would include
.NET infrastructure and tools to build and operate a new generation of services
.NET user experience to enable rich clients
.NET building block services and
.NET smart device software to enable new generation of smart, internet devices
Microsoft.NET products and services
This would include
Microsoft Windows.NET (this would have an integrated set of building block
services)
MSN.NET
Microsoft Office.NET etc.
And finally
Third-party .NET services
.NET Framework overview
This is the environment to build, test, deploy and run web services and other
.NET applications. You can build wonderful web services and web applications in
this environment but at the same time you can use this environment as
effectively to build the kind of applications that were being built so far on
Windows platform.
The 3 main parts of the .NET framework are CLR, Framework classes (class
libraries) and ASP.NET.
The .NET framework is language neutral and currently .NET programs can be build
in many languages that include C++, VB.NET, and C#.
The .NET framework gives you full access to COM+ services and makes it easy
to build serviced components.
Common Language Runtime
Common Language Runtime (CLR) is the execution engine for .NET framework
programs.
Some of the services that are provided by CLR are —
Code management
Memory management for managed objects
Memory isolation
Verification of type safety
Conversion of IL to native code
Exception handling
Interoperation between managed code, COM objects and pre-existing DLLs
Development support (profiling, debugging etc.)
CLR has Common Type System (CTS) built into it. This supports types
and operations found in most programming languages. It also supports
implementation of a wide range of languages.
The Common Language Specification (CLS) is a set of rules that a language
compiler has to adhere to, in order to create .NET programs that can run in CLR.
Microsoft Intermediate Language (MSIL) is the CPU independent instruction
set, that .NET Framework programs are compiled into. It has instructions for
loading, storing, initializing and calling methods on objects. When the program
is executed for the first time by the CLR, MSIL is converted to machine code. It
is important to note that this is not interpreted.
So from writing a program in C# and it being executed here is what happens
—
1. Source code is written in C#.
2. It is compiled using a C# compiler into an exe.
3. The C# compiler outputs the MSIL code and a manifest into a read only part of
the exe that has a standard PE (win32-portable executable) header. Note: when
compiler creates the output, it imports a function named _CoreExeMain from the
.NET runtime.
4.When the program is executed, the OS loads the PE as well as any required
DLLs.
5. The loader then jumps to entry point inside the PE (which is put there by the
C# compiler).
6. The _CoreExeMain function starts execution of the MSIL code.
7. The CLR compiles the MSIL code using just-in-time (JIT) compiler (also called
JITter) into native CPU instructions. The compiled executable code is cached on
the machine and is recompiled only if there is change to the source code.
.NET Framework Class Libraries
You can not write any real application in any .NET language without the .NET
Framework class libraries. That is because .NET languages do not have even the
basic compiler features required to do that. All this is done by the underlying
common set of class libraries.
The .NET Framework provides both abstract base classes and class
implementations derived from those base classes. You can use these derived
classes "as is" or derive your own classes from them. Also provided
are interfaces and default implementations of those interfaces. To get the
interface's functionality, you can either implement the interface yourself or
you can use or derive a class from one of the runtime-based classes that
implements the interface.
.NET Framework types are named using a dot-syntax naming scheme that connotes
a naming hierarchy. This technique is used to logically group related classes
together so that they can be searched and referenced more easily. For example,
the System.Reflection.FieldInfo class is related to other classes that also use
a System.Reflection.x naming pattern: all of the classes named with a
System.Reflection prefix can be used to discover information about types at
runtime. The part of the name up to the last dot (e.g., System.Reflection) is
often referred to as the namespace name and the last part (e.g.,
FieldInfo) as the class name. The use of naming patterns to group related
classes into namespaces is a very useful way to build and document class
libraries.
A lot of the functionality in the base framework classes resides in a
namespace called System. This is the root namespace for .NET class libraries and
some of the subsections of System are —
Namespace |
What it contains |
Example |
System.Data |
Classes and types for database management |
DataSet, DataTable, SQLConnection, ADOConnection |
System.IO |
Types for reading & writing to files and other data streams |
File, FileStream, StreamReader, StreamWriter |
System.Security |
Types that enable security capabilities |
Permissions, Policy |
System.Math |
For common mathematical functions |
Sqrt, Cos, Log, Min |
.Net Framework — how the pieces fit together
And now let us come to C#.
C#
There are a lot of interesting things in C# and we should look at all the
important design considerations of this language but at this point it would be
right to first write a simple "Hello World" program in C#.
So here is how "Hello World" looks in C#.
class HelloWorold
{
public static void main()
{
System.Console.WriteLine("Hello, World");
}
}
This can be compiled (and run) in Visual Studio.NET. But there is also a
command line compiler csc.exe in framework and since this is a console
application, it can be run in the DOS shell.
Now to examine the code —
As you can see, the method is defined in the class definition itself. This is
the only way they can be defined in C#. In C#, there are no header files, so you
can not define the class in the header file, give the declarations for the
methods there and then define the methods in a different file. Thus all the
methods have to be inline in the class definition. This way, when you write a C#
class, you get a fully encapsulated bundle of functionality that can be dropped
into any other development environment without worrying about include file
processing.
Every C# application(like C/C++ application), must have a method named Main
defined in one of the classes. Also, this method must be public and static.
public is access keyword indicating that anyone can access this method. static
indicates that this is a global method and the class does not need to be
instantiated for this method to be called. For a static method, the compiler
stores the address of the method as entry point so that the .NET runtime knows
where to begin the execution.
There is only one line of code namely —
System.Console.WriteLine("Hello, World")
This method writes the given string followed by a line terminator to standard
output.
From this line it is clear that the WriteLine method is defined in the System
namespace on the Console class. Another way to call this method would be to use
the using directive. In which case the code would look like follows —
using System;
class HelloWorold
{
public static void main()
{
Console.WriteLine("Hello, World");
}
}