Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources]

Jason Beres

نسخه متنی -صفحه : 270/ 99
نمايش فراداده

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

C#

CLS Type

Bytes

Value

Boolean

bool

System.Boolean

4

True or False.

Byte

byte

System.Byte

1

0 to 255.

Char

char

System.Char

2

0 to 65,535.

Date

DateTime

System.DateTime

8

January 1, 1 to December 31, 9999.

Decimal

decimal

System.Decimal

12

+/- 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.

Double

double

System.Double

8

-1.797693134862231E308 to -4.94065645841247 for negative values to 4.94065645841247 to 1.797693134862231E308 for positive values.

Integer

int

System.Int32

4

-2,147,483,648 to 2,147,483,648.

Long

long

System.Int64

8

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Short

short

System.Int16

2

-32,768 to 32,767.

Object

object

System.Object

4

An object can hold any variable type.

Single

float

System.Single

4

-3.402823E38 to -1.401298E-45 for negative values to 1.401298E-45 to 3.402823E38 for positive values.

String

string

System.String

10

+

(2*Length)

0 to 2 billion Unicode characters (approximately).

User-defined type (structure)

struct

System.ValueType

Sum

of

the size

of its

members

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