Integer Types
The .NET Framework provides a variety of Integer types. Table 3.1 summarizes these types and lists their corresponding Microsoft Visual Basic .NET and Microsoft Visual C# types.
Integer Types in the .NET Framework
Type Visual C# Name Visual Basic .NET Name DescriptionRange
System.Byte byte Byte 8-bit unsigned integer 0 to 255
System.Int16 short Short 16-bit signed integer -32768 to 32767
System.Int32 int Integer 32-bit signed integer -231 to 231-1
System.Int64 long Long 64-bit signed integer -263 to 263-1
System.SByte sbyte (Not implemented) 8-bit signed integer -128 to 127
System.UInt16 ushort (Not implemented) 16-bit unsigned integer 0 to 65535
System.UInt32 uint (Not implemented) 32-bit unsigned integer 0 to 232-1
System.UInt64 ulong (Not implemented) 64-bit unsigned integer 0 to 264-1
Floating-Point Types
There are three floating-point types that can be used to represent numbers that have a fractional component.
Non-Numeric Types
Four additional types that do not represent numbers are discussed in this section: System.Boolean, System.Char, System.String, and System.Object.
System.Boolean
The System.Boolean type is used to represent a value that is either true or false. It is called Boolean in Visual Basic .NET, and bool in Visual C#. The values that are valid for Boolean variables are True and False (Visual Basic .NET) or true and false (Visual C#).
System.Char
The System.Char type represents a single instance of a 16-bit Unicode character. It is called char in Visual C# and Char in Visual Basic .NET. We can assign a character literal to a variable of this type by enclosing the literal in single quotes (Visual C#) or in double quotes using the suffix "c" (Visual Basic .NET)
System.String
The System.String type is a reference type, and it represents a series of Char data types. In everyday terms, a string can represent a word, a paragraph, a key value, or any other string of characters.
System.Object
The Object type is the supertype of all types in the .NET Framework. Every type, whether value type or reference type, derives from System.Object. In Visual Basic .NET it is called Object, and in Visual C# it is called object. We can assign any object or value to an object variable.
Converting Types
At times, we will need to convert data from one type to another. Data can be converted in two ways: implicitly, which means the conversion is performed automatically, and explicitly, which means we must specifically ask for the conversion to be performed.
Implicit Conversions
Implicit conversions between types are automatically performed whenever the conversion can be performed without the loss of data. For example:
Visual C#
int AnInteger = 100;
long aLong;
// Because a long can be assigned to every possible value of integer,
// there is no chance of losing data
aLong = anInteger;
Implicit Conversions in Visual Basic .NET and Visual C#
From To
Byte (Visual Basic .NET) Short, Integer, Long, Single, Double, Decimal
byte (Visual C#) short, ushort, int, uint, long, ulong, float, double, decimal
Short Integer, Long, Single, Double, Decimal
short int, long, float, double, decimal
Integer Long, Single, Double, Decimal
int long, float, double, decimal
Long Single, Double, Decimal
long float, double, decimal
Single Double
float Double
Char Integer, Long, Single, Double, Decimal
char Int, uint, long, ulong, float, double, decimal
sbyte (Visual C# only) short, int, long, float, double, decimal
ushort (Visual C# only) int, uint, long, ulong, float, double, decimal
uint (Visual C# only) long, ulong, float, double, decimal
ulong (Visual C# only) float, double, decimal
Explicit Conversions
When performing a conversion where types cannot be implicitly converted, we must explicitly convert the types. This conversion is called a cast. Explicit conversions are accomplished in Visual Basic .NET using the CType function and using a special syntax in Visual C#.
Visual C#
long aLong = 1000;
int anInteger;
anInteger = (int)aLong;