The Primitive Types Available in C#

The list of primitive types available in C# is very similar to C and C++. One of the major reasons for the cross-language interoperability of the .NET Framework is that all languages share a common type system: the CTS. The primitive types in C# are just aliases for CTS types. Table 4-4 shows the primitive types available in C#.

Table 4-4. A Mapping of C# types to CTS types
C# TypesCLS CompliantEquivalent CTS TypeDescription
boolYesSystem.BooleanTrue or false
byteYesSystem.SByteUnsigned 8-bit number
sbyteNoSystem.ByteSigned 8-bit number
charYesSystem.Char16-bit Unicode character
intYesSystem.Int32Signed 32-bit number
uintNoSystem.UInt32Unsigned 32-bit number
longYesSystem.Int64Signed 64-bit number
ulongNoSystem.UInt64Unsigned 64-bit number
shortYesSystem.Int16Signed 32-bit number
ushortNoSystem.UInt16Unsigned 32-bit number
floatYesSystem.Single32-bit floating point number
doubleYesSystem.Double64-bit floating point number
system.DateTimeYesSystem.DateTimeDate time
decimalYesSystem.DecimalSigned 96-bit number
stringYesSystem.StringUnicode character string.
objectYesSystem.ObjectBase class for all types

Most of the familiar primitive types from C and C++ like int, long, short, char, float, and double are present in C#. C# also supports unsigned types like uint, ushort, and unlong, but keep in mind that these unsigned types are not CLS compliant. Therefore, if you do expose types from your libraries that use these unsigned types in their interface, those libraries will not be usable from many managed code languages including VB.NET. Avoid unsigned types in your interfaces if you want to write components that may be consumed by languages other than C#.

Note

The CLS specifies the features that all managed code languages must support as a minimum.


With C#, as in most managed-code languages, you can use either the language-specific alias or the CTS type name freely and interchangeably. In other words, declaring a variable to be of type “int” is exactly the same as declaring it to be of type “System.Int32”. In addition, a function that accepts a parameter of type float will also accept a parameter that is typed as System.Single.

..................Content has been hidden....................

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