9.11. Common typing with other .NET languages

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.

Additional notes:

  • 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.

    Table 9.6. C# aliases for BCL types
    C# alias.NET framework type
    boolSystem.Boolean
    byteSystem.Byte
    sbyteSystem.SByte
    charSystem.Char
    decimalSystem.Decimal
    doubleSystem.Double
    floatSystem.Single
    intSystem.Int32
    uintSystem.UInt32
    longSystem.Int64
    ulongSystem.UInt64
    longSystem.Int64
    ulongSystem.UInt64
    objectSystem.Object
    shortSystem.Int16
    ushortSystem.UInt16
    stringSystem.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());
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.170.187