Understanding CLS Data Types in Visual Basic .NET and C#

When working with data, such as setting or retrieving values from a form, you must normally take that information and either manipulate it or store it temporarily before you persist it to a database or a file.

In C# and Visual Basic .NET, you use variables to store information. Each variable that you declare is of a certain type, which is defined by the rules of the common type system (CTS). The CTS guarantees type safety between different languages, which was unheard of before .NET. That means when you use a String data type in C#, it's the same String data type that you use in Visual Basic .NET and COBOL .NET.

When I talk about different .NET languages, I mean that a language is considered a .NET language because it follows the rules of the Common Language Specification (CLS). The CTS exists in the CLS, which ensures type safety across all languages that have a .NET suffix. The allowable data types, their language-specific syntax, their CLS type, and their value ranges are listed in Table 8.1.

Table 8.1. Data Types in .NET
Visual Basic .NETC#CLS TypeBytesValue
BooleanboolSystem.Boolean4True or False.
BytebyteSystem.Byte10 to 255.
CharcharSystem.Char20 to 65,535.
DateDateTimeSystem.DateTime8January 1, 1 to December 31, 9999.
DecimaldecimalSystem.Decimal12+/- 79,228,162,514,264,337,593,950,335 with no decimal point.

+/- 7.9228162512264337593543950335 with 28 places to the right of the decimal point.

The smallest nonzero number would be a 1 in the 28th position to the right of the decimal point.
DoubledoubleSystem.Double8-1.797693134862231E308 to -4.94065645841247 for negative values to 4.94065645841247 to 1.797693134862231E308 for positive values.
IntegerintSystem.Int324-2,147,483,648 to 2,147,483,648.
LonglongSystem.Int648-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
ShortshortSystem.Int162-32,768 to 32,767.
ObjectobjectSystem.Object4An object can hold any variable type.
SinglefloatSystem.Single4-3.402823E38 to -1.401298E-45 for negative values to 1.401298E-45 to 3.402823E38 for positive values.
StringstringSystem.String10+(2*Length)0 to 2 billion Unicode characters (approximately).
User-defined type (structure)structSystem.ValueTypeSum of the size of its membersEach member of the structure has a range determined by its data type and independent of the ranges of the other members.

Note

If you're coming from a Visual Basic 6 background, notice the differences in the data type ranges. In Visual Basic .NET, the Short data type replaces the Integer data type, and the Integer data type replaces the Long data type. The Long data type in Visual Basic .NET is a 64-bit number; in Visual Basic 6, a Long data type was a 32-bit number.


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

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