Adherence to the CTS enables all .NET languages to interoperate. Having a common type system enables codes from different languages to integrate seamlessly. You can easily pass a variable of a common type between methods written in different .NET languages. You can even write a class in C# which inherits from a class written in VB .NET. One factor that makes such interoperability possible is the adherence of each .NET programming language to the CTS.
The CTS specifies a set of common types shared by all the .NET programming languages. You can either use the full .NET framework type name or the C# alias [9] in your C# programs. Table 9.6 shows the available C# aliases for types defined in the CTS.
[9] Needless to say, this will be the more popular (and recommended) choice. Who would prefer to write 'System.String' instead of 'string'?
This implies that the following two statements are equivalent, except that the first is easier to read:
ulong temp = 99L; // usage of C# alias System.UInt64 temp = 99L; // usage of full type name
Other .NET languages may have their own aliases for the .NET types too, so that writing in those languages will be more natural. For example, for VB .NET developers, their familiar Integer type maps to System.Int32. And for J# developers, J#'s int type also maps to System.Int32. VB .NET's Integer type and J#'s int type refer to exactly the same type as C#'s int – a 32-bit signed integer.
All the types listed in Table 9.6, except object and string, are referred to as simple types. Simple types are value types – on the other hand, object and string are reference types.
C# alias | .NET framework type |
---|---|
bool | System.Boolean |
byte | System.Byte |
sbyte | System.SByte |
char | System.Char |
decimal | System.Decimal |
double | System.Double |
float | System.Single |
int | System.Int32 |
uint | System.UInt32 |
long | System.Int64 |
ulong | System.UInt64 |
long | System.Int64 |
ulong | System.UInt64 |
object | System.Object |
short | System.Int16 |
ushort | System.UInt16 |
string | System.String |
To display the actual type a C# variable represents, use the GetType() [10] method of System.Object. For example, the following statement displays the system alias that represents the type of myVariable:
[10] System.Object is the superclass of all C# classes (very much like java.lang.Object). As such, all methods declared within are inherited to every C# class – GetType() is one such method.
Console.WriteLine(myVariable.GetType());
18.226.170.187