An Introduction to CIL - Classes
(Page 2 of 4 )
The basic building block of a .NET program is, of course, the class. Here's ours:
public class FindPrimes
{
...
}
The concept of classes is not just a higher-level component of .NET. Classes, of course, exist in CIL, too, and, surprisingly, they don't look much different from those in C#. The following CIL code is equivalent to the above C# code. Paste it into the file we created earlier (leaving the “...” out in order to be replaced later with meaningful code, of course):
.class public FindPrimes
{
...
}
Again, notice how similar the two are. So far, we've encountered nothing scary. The main difference here is that the protection modifier goes second in the CIL code, rather than first as in the C# code.
Methods
Now, of course, we need to add a method to our class. The syntax to create a method in CIL is, as with creating classes, not too different from the C# equivalent. Place the following CIL code inside of our class:
.method public static void Main(string[] args)
{
...
}
Again, so far, everything is simple. Observe, however, that we're not dealing with just any method here. No, we're dealing with the entry point of our program. Execution starts with this method, and while C# is able to automatically identify the method and designate it as the program's entry point based on its signature, this isn't taken care of for us with CIL. Instead, we need to manually mark this method as the entry point of our program. Fortunately, this is easy enough and only involves a few characters:
.entrypoint
So, this is what you should have in the source file so far:
.assembly extern mscorlib {}
.assembly FindPrimes {}
.class public FindPrimes
{
.method public static void Main(string[] args)
{
.entrypoint
}
}
Next: A Short Interruption >>
More ASP.NET Articles
More By Peyton McCullough