Visual C# .NET Part 2: Compiling .NET Code

In part 2, Michael Youssef explains how the .NET framework compiles .NET code, what the Just-In-Time compiler does, how to use the .NET Framework Class Library (FCL) and Common Language Runtime (CLR) to create applications. Part 1 was published in April 2004: Visual C#.NET, Part 1: Introduction to Programming Languages.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 47
June 02, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


As we said before high-level languages (like C, C++ and C#) use a compiler to compile (convert) its code to machine code language that the operating system will understand, and they use this code to execute the program. But this not exactly what happens with .NET languages. There are two steps for compiling the .NET code.

The .NET Framework compiles all .NET languages (like VB.NET, Visual C++.NET and Visual C#.NET) into Microsoft Intermediate Language (MSIL) using MSIL compiler. Remember we said that high-level languages use the compiler to compile the code into the operating system machine code and only this operating system will understand that code, but when we are compiling .NET code into MSIL code not specific to any operating system, we still need one more step. We need to compile MSIL code into the operating system specific Machine Code, and this is the job of Just-In-Time (JIT) compiler. .NET Framework use the JIT compiler to compile the Microsoft Intermediate Language code into Machine Code specific to the operating system, and now .NET applications can run on this operating system. So we have two steps: first we compile our C# code to MSIL code (which is not specific to any operating system and it’s specific to .NET Framework and only the .NET Framework can understand MSIL Code); after that we will use the JIT Compiler to Compile MSIL Code into the Machine Code.

Why are there two steps when we can have just one?

There are a number of reasons for compiling .NET applications in two steps and now we will know some of them.

All .NET languages’ code compile first to MSIL code (this is called ‘Language Interoperability’) which is specific to .NET, I mean that .NET Framework understands MSIL code and not the operating system. For example, imagine that we are working in the same company and you are Visual C#.NET programmer, other developers work with Visual C++.NET and I’m working with Visual Basic.NET. Because all these different languages’ code will compile to MSIL (as a first step), we can work together in the same project and every one of us will still work with his favorite programming language, and there is no need to learn the other languages. After we finish the project, Visual Studio.NET will compile the code to MSIL Code from the three languages after that JIT compiler will be used to compile the code to the Machine Code. If MSIL does not exist, then we can’t work together because every programming language’s compiler will not understand the other programming language code. But MSIL compiler understands all .NET languages code.

They call JIT compiler Just-In-Time compiler because it compiles the MSIL Code only when it needs to. MSIL Code compiles and saves to a file in the disk and, when we need to run the application, JIT will do its job so it’s just in time. JIT compiler is specific to the operating system and the platform, so to compile C# code on Windows NT2000 platform you need a specific JIT compiler. If you want to compile the code on another platform (like UNIX), you will need another JIT compiler specially created for that platform. 
 

Managed Code vs. Unmanaged Code


 
When we write .NET code that uses the services, functions, and capabilities of the .NET Framework Class Library (FCL), then we call that code “Managed code” because it’s managed by the Common Language Runtime (CLR). As you know, the CLR manages the execution of .NET programs, and managing the code is one way of managing .NET applications. When we write Managed Code, we don’t care about memory management or handling operating system low-level functions because CLR abstract these details from us and manages them. So writing managed code will improve applications performance.

You can write “Unmanaged Code” too. It’s the code that is not managed by the CLR, but it’s your responsibility to manage the memory and many other things. CLR can’t control this kind of code, but at the same time this code is a powerful feature because you will gain access to the low-level operating system functions and  maybe will be useful in some circumstances. But the most of us will use the managed code and benefit from CLR management and capabilities.

C# Overview

C# was created by Microsoft as the primary language for the .NET Framework. Microsoft designed C# in a way to make it easy for C, C++ and Java programmers to migrate to .NET. C# has roots in C, C++ and Java so this migration is easy for these languages’ programmers. C# syntax not complicated like C++ so learning C# is much easier that C++. Almost all of the operations that you can do with C++ you can do with C# except for the operations that access low-level operating system functions, but you still can do these operations using unmanaged code. C# is the best .NET language because it’s the only language that is designed specially for .NET Framework, and it will be the first language that will support .NET Framework versions on the other platforms.

.NET Framework Applications

With .NET Framework we can create any kind of applications that we want, but like the console applications which are simple command-line interfaces without any windows, Windows Applications which are applications with complex interface which contains buttons, menus, text boxes, option boxes and many other visual components. There are also web applications and web services, which are Internet-based applications and the core of Microsoft.NET technologies as we stated before. You will understand the different kinds of .NET applications as you go further in the .NET world. Here we will discuss the first two things (Console applications, Windows applications) only.

.NET Framework and .NET Languages


 
When we say .NET languages we mean any language that works on the .NET Framework. In other words, we can call any language that uses the .NET Framework Class Library (FCL) and is managed by the Common Language Runtime (CLR) a .NET-compliant language (languages which support .NET Platform). From these languages: Microsoft Visual Basic.NET (VB.NET), Microsoft Visual C++.NET and there are also COBOL, Eiffel, Jscript, RPG and many others. But as we stated before, C# was designed especially for .NET Framework and there are some functionalities in C# you can’t do in other languages. However, Microsoft developed Visual Basic.NET as an upgrade for Visual Basic programmers who like to work with the .NET platform.

All .NET-compliant languages will use FCL and CLR for creating applications. For example, VB.NET programmers will use the same function that C# programmers use to write a line to the console (console means the command-line screen), but of course there are difference to get this code done. Let’s look at the following .NET applications.

This is the C# application that will write a line to the console:

using System;

namespace testingApplications
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
[STAThread]
  static void Main(string[] args)
  {
   Console.WriteLine("I like C# Programming");
  }
 }
}

This is the VB.NET application that will write the same line to the console:

Module Module1

    Sub Main()
        Console.WriteLine("I like C# Programming")
    End Sub

End Module

Figure 1 illustrates the result of compiling and running the first application (C# application):


Figure 2 illustrates the result of compiling and running the second application (VB.NET application):

youssef

Here we get the same result using the same function which writes a line to the console but with different .NET languages. So all .NET languages will use the capabilities of FCL and CLR, but each language implementation differs from the others. For example, C# is case sensitive language and VB.NET is not.

Visual Studio.NET and .NET Languages

If you have worked with Visual Studio.NET before, you will notice that I used it to generate C#, VB.NET applications which we used in our example, and I just wrote a single line of code to display “I like C# programming” to the console screen. Visual Studio.NET helps you and save your time when you write applications with Microsoft VC++.NET, VB.NET, VC#.NET. So we will use Microsoft VS.NET to write applications using any of Microsoft .NET languages.

C# Application using Command-Line Compiler
 
You can compile C# applications using C# Command-Line Compiler called csc (stand for, C Sharp Compiler). I will not talk too much about C# compiler now, but I will let you create and compile a simple application using this compiler. After that we will use VS.NET to create the same application (in the next section) and you will see how much time VS.NET can save. Let’s create a simple application that will ask you for your name and print a single line containing that name.

Figure 3 contains the required lines of code for our program written using Notepad. Write these lines in a Notepad file and save it with this name “test.cs”.

It is much easier to copy and paste the next lines of code into a Notepad file instead of writing it and save it directly; you will get the same code written in Figure 4.

using System;
namespace test
{
  public class test
   {
     static void Main()
      {
 Console.WriteLine("Please Enter your name and press Enter");
 string x = Console.ReadLine();
 Console.WriteLine("{0} Likes C# Programming ",x);
 Console.ReadLine();
      }
   }
}

Important Note: C# is a case sensitive language, which means Michael is not like michael. In other words, ‘Michael’ with the first letter ‘M’ as capital letter is a different word of ‘michael’ which begins with ‘m’ as a small letter.    

Because C# is a case sensitive language, ‘using System’ is not like ‘using system’. The first one is correct, and the second one will prevent the compiler from compiling the application. So take care when you write C# code. When you read on further, you will know what you will write with capital letters and what you will write with small letters.

Compiling the Application

To compile the application, you will use the csc compiler; this compiler must run from a command-prompt with the Path environment set to access the .NET Framework programs, tools, and libraries. And because I assume that you don’t know what the Path environment is and how to set it up, I will use the Visual Studio.NET Command Prompt.

Important Note: To compile this application, you must put that file in the same hard disk drive that you will run the command prompt from. For example, if you will run your command prompt from d:/> then your file must be there in drive d.  
  
Instead of setting up the Path environment manually, we will use VS.NET to set up the Path Environment the ‘Visual Studio.NET Command Prompt’. To find this option click on start menu -> Programs -> Microsoft Visual Studio.NET -> Visual Studio.NET Tools and click on ‘Visual Studio.NET Command Prompt’. This option will open the command-Prompt window and set up the Path Environment for you.  Now we are ready to compile the application.

In the ‘Visual Studio.NET Command Prompt’ write the following line of code:

csc test.cs /reference:System.dll

And then press enter. You will find your new C# program compiled in the same directory that you run the command-prompt from with the same name of the compiled file (test.cs) but with a different extension (it will be test.exe). If you click this program, it will run and ask you about your name. After you type your name, you will get another line of code. Figure 4 illustrates compiling the application:

When you press Enter key, you will get the next message illustrated by Figure 5:

youssef

I compiled my application in drive d, so if your application compiled in drive d too, open it and click on the application icon to execute it.
 
Did you see how many steps to create very simple application with the command-line compiler? Actually we will not use it again and we will use VS.NET to create all of our applications. That will save a lot of time.

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials