This section of the Learning C# 2010 along with .NET 4 series will lay the foundation necessary to write C# and .NET applications. You will learn how .NET functions and how it makes your life as a programmer easier. An overview of how the base class library will be provided along with how .NET’s Common Intermediate Language, Common Language Runtime, Common Type System, and Common Language Specification work together to make your applications run.
Why use .NET?
.NET allows us to leverage existing code from other .NET languages and integrate them together into one language unspecific assembly. This allows programming to use many existing code samples into their applications, which can help to improve development time and decrease costs. .NET provides a single runtime engine that all .NET supported languages can use together with typical debugging and inheritance features. .NET’s extensive base class library can be leveraged to deploy more feature-rich applications in a shorter time frame.
What makes .NET tick?
.NET is primarily comprised of a runtime environment combined with a base class library. There are three main features that make .NET into the powerful solution that is it.
- CLR – Common Language Runtime
- CTS – Common Type System
- CLS – Common Language Specification
While you do now need to know what is happening under the hood of these three entities you do need to have an understanding of their purposes.
.NET’s Common Language Runtime
This is .NET’s runtime environment that is used to control aspects of .NET, such as data types. The CLR will keep memory in a stable state, it will provide application management, and thread allocation (multi CPU platforms). Another important feature of the CLR is its duty to manage security on the host machine related to your application.
.NET’s Common Type System
The CTS is utilized to define standard compliant type definitions for .NET applications. If you want your C# application to play nicely with other programming languages, you will want to make sure to use data types that the CTS states are compliant. The CTS also defines how these standard types will integrate with each other. C# allows you to utilize types that are not defined in the CTS and while you are more than welcome to take advantage of them in your scripts, be warned that you will not be able to pass these data types to a language that does not support it. You can however, utilize the type inside of a function, but not return it to another language, which the system does support.
.NET’s Common Language Specification
When envisioning the CLS, picture it as a subset of the Common Type System. It is the part of the CTS that actually makes the definitions of what is compatible and what types can be used together in unison. Through the compiler, you can test for CTS compatibility.
.NET Libraries
One of the most important features that .NET brings to programming is its base class library that supports all .NET languages. The .NET base class library provides useful classes related to database interaction, security, input & output file operations, XML integration, and even visual desktop integration. Think of the .NET base class library as a collection of code that you can leverage to develop applications more quickly and efficiently.
C#’s role in .NET
C# is Microsoft’s programming language developed specifically to provide seamless integration with .NET. While .NET does support other programming languages that have been around from before C#, Microsoft felt that they needed a new language that would be able to rival more advanced languages that their existing languages simply could not. Thus C# was born with the advanced language features that programmers needed. C# inherits from many C-style languages and even functional languages, such as LISP.
C# Compilation
When you put your C# source code through the C# compiler it is converted into an intermediate language known as the Common Intermediate Language, or CIL. This intermediate language is in the form of an .exe or dll file. Similarly, if you are using Visual Basic, your source code would be compiled into the intermediate language that would look very similar to your C# code. This is how .NET is able to allow different programming languages to work together. The intermediate language and meta data that accompanies it is setup in such a way by the compiler that it is portable between languages. When your program is run and the CLR (.NET runtime) references a piece of code in the CIL that is then compiled into instructions that can be run on your specific machine. This compiled code will look different depending on your operating system and speed of your computer. For example, mobile applications will most likely have less memory available and the runtime will take this into account before final compilation of the CIL (intermediate language) into machine specific instructions.
The method that is used to take the CIL (intermediate language) and produce the final instructions is known as the just-in-time compiler. The just-in-time compiler (JIT) produces code specific to the target machine. One important feature of the JIT is its ability to cache its output for the future if it is likely that a piece of code will be run again in the future, thus it will not need to compile it again.
The .NET System namespace in the base class library
Namespaces are used in .NET to group related libraries and types. Under the System library we have many assemblies, such as System.Collections, which deals with container types, and System.Drawing, which provides user interface tools for creating desktop apps. You cannot expect to build any useful applications in C# without referencing at least the System namespace in your applications. Namespaces can contain any number of nested namespaces. Thus we see System.Xml and System.Security, which are both nested inside of the System namespace.
This seems to be a good time to showcase our first piece of C# code. Below you can see how one would typically include these namespace into code.
using System;
using System.Collections;
public class OurArrayList {
public static void Main() {
// create ourList array
ArrayList ourList = new ArrayList();
ourList.Add("Krio");
ourList.Add("is");
ourList.Add("awesome");
}
}
This example shows how you would reference a namespace at the top of your C# code. Once you utilize the ‘using’ keyword and reference a namespace you can call the classes located inside of that namespace. In this example we are instantiating the ArrayList class into the object ourList and utilizing the method Add.
Section 1 Conclusion
This first section in the Learning C# 2010 along with .NET 4 series has provided you with a firm foundation related to gaining an understanding of what happens in the background when you create C# applications with .NET. In addition, you have a functional understanding of the base class library and how to integrate a namespace.


