Friday, 4 March 2011

Chapter 1 Basic C# Introduction

Basics of C#


C# is a very powerful language yet it has only 90 or so keywords. According to Microsoft it is easy to learn specially for people with background in C, C++, and Java. It has all the bells and whistles that you expect from a modern language.As this is an object oriented language therefore it supports encapsulation,polymorphism and inheritance.People who are familiar with C and C++ will find their old friend struct in C#. As we all know C# is a completely new language thus it is free from backward compatibility curse and cuts quite a bit of flab from other languages.
The C# build process is simple compared to C and C++ and more flexible than in Java. There are no separate header files, and no requirement that methods and types be declared in a particular order. A C# source file may define any number of classes, structs, interfaces, and events.


.NET Framework


Before we delve deeper into C# we must understand a little bit about .Net Framework.
This language works on the .NET Framework which in turn is the main component of Windows that includes a virtual execution system called the common language runtime (CLR) and a unified set of class libraries. The CLR is Microsoft's commercial implementation of the common language infrastructure (CLI), an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.

Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification. The IL code, along with resources such as bitmaps and strings, is stored on disk in an executable file called an assembly, typically with an extension of .exe or .dll. An assembly contains a manifest that provides information on the assembly's types, version, culture, and security requirements.
When the C# program is executed, the assembly is loaded into the CLR, which might take various actions based on the information in the manifest. Then, if the security requirements are met, the CLR performs just in time (JIT) compilation to convert the IL code into native machine instructions. The CLR also provides other services related to automatic garbage collection, exception handling, and resource management. Code that is executed by the CLR is sometimes referred to as "managed code," in contrast to "unmanaged code" which is compiled into native machine language that targets a specific system. The following diagram illustrates the compile-time and run time relationships of C# source code files, the base class libraries, assemblies, and the CLR

Let us now examine each of these parts on their own to get a better understanding of what is going on under the hood.


MSIL

We write our program in C# which is in normal english, when this program or source code is compiled, it is converted into an Intermediate Language or Microsoft Intermediate Language called MSIL. If we want to see this code in human readable format Microsoft provides us with an utility. The ILDasm (Intermediate Language Disassembler) program that ships with the .NET Framework SDK (FrameworkSDK\Bin\ildasm.exe) allows the user to see MSIL code in human-readable format. By using this utility, we can open any .NET executable file (EXE or DLL) and see MSIL code.


JIT (Just In Time Compilers)


As and when we require an executable code for a machine the MSIL code is fed into JIT by the runtime(.net). This compiler JIT produces the exe or dll for the native machine. We must understand that JIT is quite different from all old fashioned compilers. JITs only compile the IL as and when required or called to do so. Therefore the part of the program needed at that moment only that part is compiled and converted in native code the rest is not touched. If CLR needs the same code again then it re-uses already compiled native code without re-compiling. Some people say this is a slow way of doing things, but as you can see the longer the program runs the lesser is the penalty associated with JIT.

.NET Framework Class Libraries


These are all the utility classes that Microsoft provides that you can use in your program to build upon. you can use only those classes that are needed. FCL are the biggest collection of libraries ever to be shipped with any known language thus it covers all your needs and are highly optimized for the user. It covers window programming, IO, File Operation, thread, Sockets, strings, security,network. You name it and this has it.

Common Type System


.Net defines CTS or common type system which describes the basic data types used in MSIL. This helps in communication between two languages. an example in int32 defined by MSIL which is mapped to int in C# and Integer in Visual Basic

Common Language Runtime (CLR)

The core runtime engine in the Microsoft .NET Framework for executing applications. The common language runtime supplies managed code with services such as cross-language integration, code access security, object lifetime management, resouce management, type safety, pre-emptive threading, metadata services (type reflection), and debugging and profiling support. The ASP.NET Framework and Internet Explorer are examples of hosting CLR.
The CLR is a multi-language execution environment. There are currently over 15 compilers being built by Microsoft and other companies that produce code that will execute in the CLR.
The CLR is described as the "execution engine" of .NET. It's this CLR that manages the execution of programs. It provides the environment within which the programs run. The software version of .NET is actually the CLR version.