Monday 30 April 2012

About C Sharp

What is C# ??

The origins of the C# programming syntax can be traced right back to C and C++. If you are already familiar with C or C++ then you have a big head start in terms of learning C#. In fact the same can be said of syntax similarities between Java, C, C++ and C# syntax. In addition, C# also inherits many of the benefits of Java in terms of memory handling (better known as garbage collection) and an intermediate byte code that negates the need to recompile an application for each target hardware platform.

C# is also accompanied by a vast framework of libraries designed to provide the programmer with ready-made solutions to just about every imaginable scenario. Despite these similarities there are differences between the Java and C# infrastructures. The remainder of this chapter will be dedicated to providing an overview of the C# infrastructure.

C# is

Flexible: C# programs can execute on the current machine, or they can be transmitted over the Web and executed on some distant computer.

Powerful: C# has essentially the same command set as C++, but with the rough edges filed smooth.

Easier to use: C# modifies the commands responsible for most C++ errors so you spend far less time chasing down those errors.

Visually oriented: The .NET code library that C# uses for many of its capabilities provides the help needed to readily create complicated display frames with drop-down lists, tabbed windows, grouped buttons, scroll bars, and background images, to name just a few.
Internet friendly: C# plays a pivotal role in the .NET Framework, Microsoft’s current approach to programming for Windows, the Internet, and beyond. .NET is pronounced dot net.
Secure: Any language intended for use on the Internet must include serious security to protect against malevolent hackers.
Finally, C# is an integral part of .NET.Secure:


About C # Essentials

C# is the latest progression in a never ending quest to make it as easy and efficient as possible for humans to program computers. Whilst it would be easy to simply describe C# as just another object oriented programming language developed by Microsoft (and ratified by ECMA and ISO), the fact is that C# is actually an integral part of an entire development and execution infrastructure. The primary object of this chapter of C# Essentials is to provide an overview of both the C# language and the infrastructure on which it relies. By the end of this chapter it also is intended that the reader will have a clear understanding of what acronyms such as CLI, CLR, VES, JIT and .NET mean.

The C# is actually an integral part of an entire development and execution infrastructure. The primary object of this chapter of C# Essentials is to provide an overview of both the C# language and the infrastructure on which it relies. By the end of this chapter it also is intended that the reader will have a clear understanding of what acronyms such as CLI, CLR, VES, JIT and .NET mean.

The Common Language Infrastructure (CLI)

C# is an object oriented programming language. It is essentially a standard defining what constitutes valid syntax. On its own C# is actually of little use because it is dependent upon something called the Common Language Infrastructure (CLI) both for compilation and execution of applications. The CLI in turn, is actually a standard which defines specifications for the following components:

  • Virtual Execution System (VES)
  • Common Intermediate Language (CIL) 
  • Common Type System (CTS) 
  • Common Language Specification (CLS) 
  • Framework 

In the remainder of this chapter we will look at each of these CLI components in order to build up a picture of how the CLI environment fits together.

Common Intermediate Language (CIL)

Unlike the C and C++ compilers which compile source code down to the machine code understood by the target microprocessor, the C# compiler compiles to an intermediate byte code format known as the Common Intermediate Language (CIL). This code can, in theory, be taken to any system where there is a CLI compliant Virtual Execution System (VES) and executed. There is, therefore, no need to compile an application for each and every target platform.

The word Common in Common Intermediate Language is used because this format is common to more than just the C# programming language. In fact any programming language may target the CIL allowing libraries and code modules from different languages to execute together in the same application. Typical languages for which CIL compilation is available include Visual Basic, COBOL, SmallTalk and C++.

Virtual Execution System (VES)

The VES (usually referred to as the runtime) is the environment in which the CIL byte code is executed. The VES reads the byte code generated by the C# compiler and uses something called a Just in Time (JIT) compiler to compile the byte code down to the native machine code of the processor on which it is running. While this code is executing it does so in conjunction with a runtime agent which essentially manages the execution process. As a result, this executing code is known as managed code and the process handles issues such as garbage collection (to handle memory allocation and de-allocation), memory access and type safety to ensure that the code does not do anything it is not supposed to do.

A term that is often used in connection with the VES is the Common Language Runtime (CLR). The CLR is officially the name given to Microsoft's implementation of the VES component of the CLI specification.

It is worth noting that the JIT process can introduce a startup delay on execution of an application. One option available with .Net to avoid this problem is to pre-compile CLI byte code down to native machine code using the NGEN compiler. Because the NGEN compilation must take place on the target processor architecture this step is often performed at the point that the application in question is installed by the user.

Common Type System (CTS) & Common Language Specification (CLS)

As mentioned previously a number of different programming languages target the CLI allowing, for example, code from C# sources to interact with code from Visual Basic. In order to achieve this feat, each language must have the same concept of how data types are stored in memory. The CTS, therefore, defines how a CLI compatible language must view the bit patterns of values and layout and behavior of objects to ensure interoperability.

The CLS is essentially a subset of the CTS aimed at creating interoperable libraries.

The Framework (Base Class and Framework Class Libraries)

The CLI specifies a set of base classes that must be available to executing CLI code, otherwise known as the Base Class Library (BCL). The BCL contains APIs that enable executing CIL code to interact with the runtime environment and the underlying operating system.

Beyond the basics there is also the Framework Class Library. This is a Microsoft library which contains APIs for the creation of graphical user interfaces, database applications, web access and much, much more.

Non Microsoft Implementations of the CLI


Microsoft's implementation of the CLI stack is called .NET. .NET is not, however, the only implementation available. Another implementation provided by Microsoft is called Rotor. Rotor is available for Windows, Mac OS and FreeBSD and is available in source form. Rotor, however, is primarily a learning tool and as such is licensed under terms which prohibit use as the basis of commercial applications. Other significant open source implementations are the Mono and DotGNU projects targeted at Windows, Linux and UNIX platforms.