Home > Blogging, C#, Development, Microsoft, Programming, Tutorials > C# Programming #2: Type Overview

C# Programming #2: Type Overview

The C# programming language is a strongly-typed language. Meaning that every variable, every class, every constant is a Type, including any expression that evaluates into a value. Every method in C# uses Types as return values, and method parameters supplied are Types.

The .NET Framework library includes hundreds of classes that represent a wide variety of simple and complex Types. Wait, a class represents a Type? What’s that mean? Well, a class encapsulates your source code, it contains all the code that makes your Type work. Think of it in this light, a class is the blueprint, a Type is the finish product.

You probably know that most software is compiled into 1’s and 0’s right? Well Microsoft has done something interesting, they’ve create an intermediary of sorts. While your programming language of choice might be C# or Visual Basic.NET, there is yet one more language included in the .NET Framework and that’s the Common Intermediate Language (or CIL for short).
CIL is not 1’s and 0’s, but rather byte code. It’s still humanly readable, (albeit not easily understandable), and is what is eventually compiled into 1’s and 0’s. At first this seems like an extra step, but I’m going to explain why it’s done this way.
So, you’re language of choice is C#? You compile your application and it gets compiled into CIL code. What if your friend makes an add-on for your application, but he wrote it in VB? His add-on will still be compatible with your C# application due to the fact that his VB source code will be compiled into CIL as well. Once the C# and the VB codes are in CIL, they are considered equal, they are the same language. Even though your .exe file is sitting there looking at you, it’s not really in 1’s and 0’s for the computer to read, it’s in CIL. When you start your application, the .NET Framework immediately compiles the CIL code into 1’s and 0’s, so that your application runs as a Native app.

What’s this have to do with Types? Well, like I mentioned before, a class contains all your source code, it acts as a blueprint. When you compile your source code into CIL, the class becomes a compiled Type. A finished product. What’s this mean for you? It means that your finished product, your Type, can now be used within your software or anyone else’s software. What if you are writing your application, and you write a class, and you need to use your class for your application, does it need to be compiled separately from your program in order to be used? Not at all! For an example, lets assume we have a class called Car and your program wants to use the Car to let people drive it. You have the class, but you don’t have a compiled Type yet, not only that, but the Car is part of your program and thus you can’t get access to the compiled Type right? You only have access to the class? (blueprint in source). Well that’s actually incorrect, as the C# compiler will see that your application needs to use the Car class, and will go ahead and compile it into a Type while it compiles the rest of your program, allowing your compiled program to use the Type internally.

Hopefully I did a decent job of describing the difference between Types and Classes, if not, feel free to drop a comment below. Just remember as we go through the remaining tutorials in this series, that when I mention class, it refers to your source code, and when I say Type, it refers to the actual finished object.

I will explain more on Types as I get into creating Properties, Fields and Methods.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: