.NET Type System, Part I
(Page 1 of 6 )
Learn how to write better .NET and C# applications! .NET allows you to store simple values and complex values. This article explains how, as well as how to use the ILDASM.EXE tool (Intermediate Language Disassembler) to inspect MSIL compiled .NET files. It also gives a detailed explanation of how and where the values are stored in your programs.
In this article you will learn about Value Types and Reference Types, and when you complete the article you will have an in-depth understanding of how and where the values are stored in your programs, which will help you write better .NET and C# applications. You will learn about the Stack and the Managed Heap, too. You will use the ILDASM.EXE tool (Intermediate Language Disassembler) to inspect the MSIL compiled .NET files (which we call Assemblies). As for Value Types, you will learn about Built-In Data Types, Enumerations and Structures. As for Reference Types, you will learn about Arrays and Strings for now. There are also Classes and Delegates, but I will just introduce them here and discuss each of them in its own article. So let's get started with a look at the .NET solution for storing simple values and complex values.
The .NET Types, again
Every time you write C# applications you will probably be defining Types. As we discussed in the previous article, Introducing C# and the .NET Framework, a Type is a representation to a value and its associated operations, but let's try to divide the types of values that we can have in our applications.
Every programming language has its own set of built-in data types such as integer numbers, floating point numbers and strings, and user defined data types such as classes and structures. This is great, but it's essential for you as a developer to understand what happens when you use these types, and what are the expected behaviors from each type. Say that at a given point in your application you want to store a student's total grade (which is 98) in a variable of type int (integer), so you will use the following statement:
int TotalGrade = 98;
What happens here is that the CLR allocates a four byte memory location of type System.Int32 and stores the value 98 there, and gives this location the name TotalGrade. What you need to know at this point is that:
- The location that the CLR has allocated to the variable TotalGrade is Strongly-Typed, which means that only the defined operations on Int32 type are permitted for this location.
- The TotalGrade contains a simple value which is 98 so the value is stored in the variable's memory location.
The TotalGrade variable is a Value Type, which means that the value 98 is stored in-line (in the variable location). .NET actually defines another kind of Type called Reference Types. We will discuss Value and Reference Types in this article, but for now think of a Reference Type as a type that stores a reference to another memory location where the actual data is stored. So the .NET Type system is built on the idea of types whose variables contain the value (Value Types) and types whose variables contain a reference to another memory location where the actual value is stored (Reference Types). Table 2.1 illustrates the CTS Type System.
| Reference Types | Value Types |
Classes Delegates Arrays * Strings * Interfaces
| Primitive Types * Structures * Enumerations *
|
* means that this Type will be discussed in this article, others will be discussed in later articles.
Before we discuss Value Types and Reference Types, I want to introduce the ILDASM.EXE tool.
Next: The ILDASM.EXE Tool >>
More .NET Articles
More By Michael Youssef