November 26, 2014
Hot Topics:

Programming with C# - - 101

  • November 19, 2001
  • By Bradley L. Jones
  • Send Email »
  • More Articles »

The benefit of creating programs for a runtime environment is portability. In older languages such as C and C++, if you wanted to create a program that could run on different platforms or operating systems, you had to compile different executable programs. For example, if you wrote a C application and you wanted to run it on a Linux machine and a Windows machine, you would have to create two executable programs[md]one on a Linux machine and one on a Windows machine. With C#, you create only one executable program, and it runs on either machine.

If you want your program to execute as fast as possible, you want to create a true executable. A computer requires digital, or binary, instructions in what is called machine language. A program must be translated from source code to machine language. A program called a compiler performs this translation. The compiler takes your source code file as input and produces a disk file containing the machine language instructions that correspond to your source code statements. With programs such as C and C++, the compiler creates a file that can be executed with no further effort.

With C#, you use a compiler that does not produce machine language. Instead it produces an Intermediate Language (IL) file. Because this isn't directly executable by the computer, you need something more to happen to translate or further compile the program for the computer. The CLR or a compatible C# runtime does this final compile just as it is needed.

One of the first things the CLR does with an IL file is a final compile of the program. In this process, the CLR converts the code from the portable, IL code to a language (machine language) that the computer can understand and run. The CLR actually compiles only the parts of the program that are being used. This saves time. Additionally, after a portion of your IL file has been given a true compile on a machine, it never needs to be compiled again, because the final compiled portion of the program is saved and used the next time that portion of the program is executed.

Compiling C# Source Code

To create the IL file, you use the C# compiler. You typically use the csc command to run the compiler, followed by the name of the source file. For example, to compile a source file called radius.cs, you type the following at the command line:

csc radius.cs

If you're using a graphical development environment, compiling is even simpler. In most graphical environments, you can compile a program by selecting the compile icon or selecting the appropriate option from the menu. After the code is compiled, selecting the run icon or selecting the appropriate option from the menus executes the program. You should check your compiler's manuals for specifics on compiling and running a program.

After you compile, you have an IL file. If you look at a list of the files in the directory or folder in which you compiled, you should find a new file that has the same name as your source file, but with an .exe (rather than a .cs) extension. The file with the .exe extension is your "compiled" program (called an assembly). This program is ready to run on the CLR. The assembly file contains all the information that the common runtime needs to know to execute the program.

Figure 1.1 shows the progression from source code to executable.

Figure 1.1.The C# source code that you write is converted to Intermediate Language (IL) code by the compiler.



Page 3 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel