The C# Language
C# (Pronounced C-Sharp) is not doubt the language of choice in the .Net environment. It is a whole new language of the backward compatibility curse with a whole bunch of new, exciting and promising features. It is an Object Oriented Programming language and has at its core, many similarities to Java, C++ and VB.In fact, C# combines the power and efficiency of C++, the simple and clean OO design of Java and the language simplification of Visual Basic.
Like Java, C# also does not allow multiple inheritance or the use of pointers (in safe /managed code), but does provide garbage memory collection at run time, type and memory access checking. However, contrary to JAVA, C# maintains the unique useful operations of C++ like operator overloading, enumerations, pre-processor directives, pointers (in Unmanaged/un-safe code), function pointers (in the form of delegates) and promises to have template support in the next versions.Like VB.it also supports the concepts of properties (context sensitive fields). In addition to this, C# comes up with some new and exciting features such as reflections, attributes, marshaling, remoting,threads,streams, data access with ADO.Net and more.
The .Net Architecture and .Net Framework
In the .net Architecture and .Net Framework there are different important terms and concepts which we will discuss one by one:-
The Common Language Runtime (CLR)
the most important concept of the .Net Framework is the existence and functionality of the .Net Common Language Runtime (CLR), also called .Net Runtime for short. It is a framework layer that resides above the OS and handles the execution of the .Net applications. Our programs don't directly communicate with the OS but go Through the CLR.

MSIL (Microsoft Intermediate Language) Code
When we compile our .Net Program using any .Net compliant language (such as C#, VB.Net or C++.Net) our source code does not get converted into the executable binary code, but to an intermediate code known as MSIL which is interpreted by the Common Language Runtime .
MSIL is operating system and hardware independent code. Upon program execution, this MSIL
(intermediate code) is converted to binary executable code (native code). Cross language relationship are possible as the MSIL code is similar for each .Net language.

Just In Time Compilers (JITers)
When our IL compiled code needs to be executed, the CLR invokes the JIT compiler, which compilethe IL code to native executable code (.exe or .dll) that is designed for the specific and OS. JITers in many ways are different from traditional compilers as they compile the IL to to native code only when desired; e.g., when a function is called, the IL of the function's body is converted to native code just in time. So,the part of code,that is not used by that particular run is never converted to native code. If some IL code is converted to native code,then the next time it's needed,the CLR reuses the same (already compiled) copy without re-compiling. So, If a program runs for some time (assuming that all or most of the functions get called), then it won't have any just-in-time performance penalty.
As JITers are aware of the specific processor and OS at runtime, they can optimize the code extremely efficiently resulting in very robust application. Also, since a JIT compiler knows the exact current state of executable code, they can also optimize the code by in-lining small function calls (like replacing body of small function when its called in a loop, saving the function call time). although Microsoft stated that C# and .Net are not competing with language like C++ in efficiency and speed of execution, JITers can make your code even faster than C++code in some when the program is run over an extended period of time (like web-servers).
The Framework Class Library (FCL)
The .net Framework provides a huge Framework (or Base) Class Library (FCL) for common, usual tasks. FCL contains thousands of classes to provide access to Windows API and common functions like String manipulation, Common data Structures, IO, Streams, Threads, Security,Network Programming,Windows Programming, web Programming, Data Access, etc. It is simply the largest standard library ever shipped with any development environment or programming language. The best part of this library is they follow extremely efficient OO design (design patterns) making their access and use very simple and redictable you can use the classes in FCL in your program just as you would use any other class. you can even apply inheritance and polymorphism to these classes.
The Common Language Specification (CLS)
Earline, We used the term .Net Compliant Language'and stated that all the .Net compliant languages can make use of CLR and FCL. But what makes a language a .Net compliant' language? The answer is the Common Language Specification (CLS). Microsoft has released a small set of specifications that each language should meet to qualify as a .Net Compliant Language. As IL is a very rich language, it is not necessary for a language to implement all the IL functionality; rather, it merely need to meet a small subset of CLS to qualify as a .Net compliant language. This is the reason why so many languages (procedural and OO) are now running under the .Net umbrella. CLS basically addresses language design issues and lays down certain standards. For instance, there should't be any global function declarations, no pointers, no multiple inheritance and things like that. The important point to note here is that if you keep your code within the CLS boundary, your code is guaranteed to be usable in any other .Net language.
The Common Type System (CTS)
.Net also defines a Common Type System (CTS). Like CLS, CTS is also a set of standards. CTS defines the basic data types that IL understands. Each .Net compliant language should map its data types to these standard data types. This makes it possible for 2 languages to communicate with each other by passing/receiving parameters to and from each other. For example, CTS defines a type, Int32, an integral data type of 32 bits (4 bytes) which is mapped by C# through int and VB.Net through its Integer data type.
Garbage Collection (GC)
CLR also contains the Garbage Collector (GC), which runs in a low- priority thread and checks for un-referenced, dynamically allocated memory space. If it finds some data that is no longer referenced by any variable/reference, it re-claims it and returns it to the OS. The presence of a standard Garbage Collector frees the Programmer from keeping track of dangling data. Ask any C++ programmer how big a relief it is!
The .Net Framework
The .Net Framework is the combination of layers of CLR, FCL, Data and XML Classes and our Windows, Web applications and web Services. A diagram of the .Net Framework is presented below for better understanding.

C# compared to C++
In terms of performance and efficiency in the use of memory and other resources, C++ does outclass C#. But are performance and speed the only measure when it comes to choosing a development environment? No! C++ is no doubt a very complex, abstract and low-level language to program. It burdens programmer with many responsibilities and less support. Another problem with C++ is that it is a vast language at its core, with too many language constructs, and many of those are very repetitive; e.g.,to handle the data in memory you can either use variable, pointer or reference. C# at its core is very simple yet powerful modern language which has been made while keeping mind the experience of developers over the years and their current needs. One of the biggest concerns in the development market is to make programs that are re-usable, maintainable, scalable, portable and easy to debug. C# comes right with these issues. Every single C++ developer knows how difficult it is to manage bigger C++ program and debug them. It can be a nightmare to find the reason why a program crashes randomly. The sole reason for this,to me, is the backwards-compatibility curse. They made C++ on the structure of C, a structured programming language, so it never became a true object oriented programming language only, and if the compiler allows me to go in the structured way of solving problem, who are you to make me take an object oriented approach? Also Bjarne Stroustrup, the founder of C++ said, "C++ is a multi-paradigm language not only OO language". The prime advantages of using C# include support for the common language runtime, Framework class library, and a new,clean object oriented design free of the backwards-compatibility curse.
The Visual Studio.Net IDE
Microsoft Visual Studio.Net is an Integrated Development Environment (IDE), which is the successor of Visual Studio 6. It eases the development process of the .Net Application (VC#.Net, VB.Net, VC++.Net, J Script. Net, J#.Net, ASP.Net, and more). The revolutionary approach in this new improved version is that for all the Visual Studio.Net Compliant Language use the same IDE, debugger, project and solution explorer, class view, properties tab, tool box, standard menu and toolbar. The Key features of Visual Studio.Net include: The IDE Provides various useful development tools such as:
- Keyword and syntax highlighting
- Intellisense (auto complete), which helps by automatically completing the syntax as you type a dot(.) with object, enumerations, namespaces and when you use the "New" keyword.
- Project and solution management with solution explorer that helps to manage application consisting of multiple files.
- Help building user interface with simple drag and drop support.
- Properties tab that allow you to set different properties for multiple windows and web controls.
- Standard debugger that allows you to debug your program using putting break points for observing run-time behavior.
- Hot compiler that checks the syntax of your code as you type it and error notification.
- Dynamic Help on a number of topics using the Microsoft Development Network (MSDN) library.
- Compiling and building applications.
- Program Execution with or without the debugger.
- Deploying your .Net application over the Internet or to disk.
Projects and Solutions
A Project is a combination of executable and library files that make an application or module. A project's information is usually placed in a file with the extension '.csproj' where '.cs' represents C-Sharp. Similarly, VB.Net projects are stored as 'vbproj' files. There are several different kinds of projects such as Console Application Windows Applications, ASP.Net Web Application, Class Libraries and more.
A solution on the other hand is a placeholder for different logically related projects that make some application. For example, a solution may consist of an ASP.Net Web Application project and a Windows Form project. The information for solution is stored in '.sln' files and can be managed using Visual Studio.Net Solution Explorer. Solutions are similar to VB 5,8's Project Group and VC++ 5,8's workspace
Toolbox, Properties and Class View Tabs
Now there is a single toolbox for all the Visual Studio.Net's languages and tools. The toolbox (usually present on the left hand side) contains a number of common controls for windows, web and data application like the text box, check box,tree view, list box, menus, file open dialog, etc.
- The Properties Tab (usually present on the right hand side in the IDE) allows you to set the properties on controls and forms without getting into code.
- The Class View Tab shows all the classes that your project contains along with the methods and fields in tree hierarchy. This is similar to VC++ 5,8s class view.
Writing your First Hello World Console Application in C#
In the following text, we will build our first C# application with, and then without, Visual Studio.Net. We will see how to write, compile, and execute the C# application. Later in the chapter, we will explain the different concepts in the program.
Working Without Visual Studio.net
Open Notepad, or any other text editor, and write the following code:

Save this with any file name with the extension ".cs".Example: 'MyFirstApplication.cs' To compile this file, go to command prompt and write:

This will compile your program and create an .exe file (MyFirstApplication.exe) in the same directory and will report any error that may occur.
to run your program, type:

This will print Hello World as a result on your console screen. Simple, isn't it? Let's do the same procedure with Visual Studio.Net:
With Visual Studio.Net
Start Microsoft Visual Studio.Net and select File-New-Project; this will show the open file dialog. Select Visual C# Project from Project type and select Console Application from template. Write MyHelloWorldApplication in the name text box below and Click OK.

This will show you the initial default code for your hello world application:

Change the name of class from Class Program to HelloWorld and Write this code.

to make the picture look like

Now to compile and execute your application, select Debug-Start Debugging or press Ctrl+F5. This will open a new Console Window with Hello World written in it. Once you press any key, the window will close, terminating the program.
Understanding the Hello World Application Code:
The first line of our program (using System;) appears in virtually all the C# programs. it gives us access to the core functionality of our computer system. We will discuss this a bit later. Let's first see what the second line (namespace MyHelloWorldApplication) means.
Namespaces in C#
A Namespace is simply a logical collection of related classes in C#. We bundle our related classes (like those related with database activity) in some named collection calling it a namespace (e.g., DataActivity). As C# does not allow two classes with the same name to be used in a program, the sole purpose of using namespaces is to prevent name conflicts. The may happen when you have a large number of classes, as is the case in the Framework Class Library (FCL). It is very much possible that our Connection Class in DataActivity conflicts with the Connection Class of InternetActivity. To avoid this, these classes are made part of their respective namespace. So the fully qualified name of these classes will be DataActivity.Connection and InternetActivity.Connection, hence resolving any ambiguity for the compiler.
So, in the second line of our program we are declaring that the following classes (within { } block) are of MyHelloWorldApplication namespace.

The C# namespace have NO physical mapping as is the case in Java. Classes with same namespace can be in different folders. The C# concept of mapping is similar to packages in Java and namespaces in standard C++. The namespace may contain classes, events, exceptions,delegates and even other namespaces called 'Internal namespace'.
These internal namespaces can be defined like this:

The using Keyword
The first line of our program was:

The using keyword above allows us to use the classes in the following 'System' namespace. By doing this, we can now access all the classes defined in the System namespace like we are able to access the Console class in our Main method later. One point to remember here is using allows you to access the classes in the referenced namespace only and not in its internal/child namespaces. Hence we might need to write

in order to access the classes defined in Collection namespace which is a sub/internal namespace of System namespace.
The class Keyword
All of our C# programs contain at least one class. The Main() method resides in one of these classes. Classes are a combination of data (fields) and functions (methods) that can be performed on this data in order to achieve the solution to our problem. We will see the concept of class in more detail in the coming days. Classes in C# are defined using the Class keyword followed by the name of class.
The Main() Method
In the next line we defined the Main() method of our program:

This is the standard signature of the Main method in C#. The Main method is the entry point of program i.e., our C# program starts its execution from the first line of method and terminates with the termination of Main method. The Main method is designate as static as it will be called by the CommonLanguage Runtime (CLR) without making any object of our HelloWorld Class (which is the definition of static method, fields and properties). The method as also declared Void as it does not return anything. Main is the (standard) name of this method, while String [] args is the list of parameters that can be passed to main while executing the program from command line. We will see this later.
One interesting point here is that it is legitimate to have multiple main() methods in C# program. But you have to explicitly identify which Main method is the entry point at the run-time. C++ and Java Programmers, take note that Main starts with capital 'M' and the return type is Void .
Printing on the Console
Our next line prints Hello World on the Console screen:

Here we called WriteLine(),a static method of the Console class defined in the System namespace.
This method takes a string (enclosed in double quotes) as its parameter and prints it on the Console
window.
C# like other object Oriented languages, uses the dot (.) operator to access the member variable
(fields) and methods of a class. Also, braces () are used to identify methods in the code and string
literals are enclosed in double quotation marks (").Lastly, each statement in C# (like C,C++ and Java)
ends with a semicolon (;), also called the statement terminator.
Comments
Comments are the programmer's text to explain the code, are ignored by the compiler and are not
included in the final executable code. C# uses syntax for comments that is similar to Java and C++.
The text following double slash marks (// any comment) are line comments. The comment ends with
the end of the line:

C# also supports the comment block. In this case, the whole block is ignored by the compiler. The
start of the block is declared by slash-asterisk (/) and ends with asterisk-slash mark (/):

C# introduces another kind of comment called 'documentation comments'. C# can use these to generate
the documentation for your classes and program. These are line comments and start with triple slash
mark (///):

We will discuss these in detail in coming issues.
Important points to remember
- Your C# executable program resides in some class.
- The entry point to program is the static method Main() with void return type.
- C# is a case sensitive language so void and Void are different.
- Whitespaces (enter, tab, space) are ignored by the compiler between the code. hence, the following is also a valid declaration of the Main() method although it is not recommended:

- You DON'T need to save your program with same file name as of your class containing Main() method.
- There can be multiple Main() methods in your program.
- The boundaries of namespace, class and method are defined by opening and closing curly bracket { }.
- A namespace is only logical collection of classes with no physical mapping on disk (unlike Java).
- The using keyword is used to inform compiler where to search for the definition of classes (namespaces) that you are about to use in your C# program.
- The three types of comments exits in C#,line, block and documentation. These are ignored by the compiler and are used only to enhance the readability and understandability of program for the developers.
- Enclosing your class in some namespace is optional. You can write program where your class is not enclosed by any namespace.
- it is not mandatory that Main Method of program takes 'string[] args' as parameter.It is perfectly
- valid to write Main method as:

A more interactive Hello World Application
Up until now, we have seen a very static hello world application that greets the whole world when it is executed. Let's now make a more interactive hello world that greets the current user of it. This program will ask the user their name and will greet using his/her name, like 'Hello Sachin Chaudhary' when a user named 'Sachin Chaudhary' runs it. Let's see the code first:

Author's Note: In the above code, we haven't shown the complete program but only the Main Method
to save space.We will follow this strategy in the rest of the course when appropriate.
Discussing a more interactive Hello World Application
In the first line, we have used another method, Write(), of the Console class. This is similar to theWriteLine() method, discussed in the previous program, but does not change the line after printing the string on the console.
In the second line, we declared a variable of the type string and called it 'name'. Then, we took a line of input from the user through the ReadLine() method of the Console class and stored the result in the
'name' variable. The variables are placeholders in memory for storing data temporarily during the
execution of program. variables can hold different types of data depending on their data-type, e.g., int variables can store integers while string variables can store a string (series) of characters. The ReadLine() method of the console class (contrary to WriteLine()) reads a line of input given at the Console Window. It returns this input as string data, which we stored in our string variable 'name'
Author's Note: A string is implicit data in C# contrary to other languages. It starts small 's'.
In the third line, we printed the name given by user in line 2, also with some greeting text using theWriteLine() method of the Console class. Here we used the substitution parameter {0} to state where in the line the data in the variable 'name' should be written when the WriteLine() method is called.

When the compiler finds a substitution parameter, {n}, it replaces it with the (n+1)th variable following the string. The string is delimited with double quotation marks and each parameter is separated by a comma. Hence, in our case when the compiler finds {0}, it replaces it with (0+1)th, i.e., the 1st variable ('name') following the string. So at run-time, the CLR will read it as:

if the value of 'name' is Sachin Chaudhary at run-time. Alternatively, it can also be written as

removing the substitution parameter. Here we concatenate (add) the strings together to from a message. (the first approach is similar to C's printf() function while the second is similar to Java'sSystem.out.printline() method) when we compile and run this program the output will be:
