Sunday, August 23, 2009

Predefined types

1.1 Predefined types

C# provides a set of predefined types, most of which will be familiar to C and C++ developers.

The predefined reference types are object and string. The type object is the ultimate base type of all other types.

The predefined value types include signed and unsigned integral types, floating point types, and the types bool, char, and decimal. The signed integral types are sbyte, short, int, and long; the unsigned integral types are byte, ushort, uint, and ulong; and the floating point types are float and double.

The bool type is used to represent boolean values: values that are either true or false. The inclusion of bool makes it easier for developers to write self-documenting code, and also helps eliminate the all-too-common C++ coding error in which a developer mistakenly uses “=” when “==” should have been used. In C#, the example

int i = ...;
F(i);
if (i = 0) // Bug: the test should be (i == 0)
G();

is invalid because the expression i = 0 is of type int, and if statements require an expression of type bool.

The char type is used to represent Unicode characters. A variable of type char represents a single 16-bit Unicode character.

The decimal type is appropriate for calculations in which rounding errors are unacceptable. Common examples include financial calculations such as tax computations and currency conversions. The decimal type provides 28 significant digits.

The table below lists each of the predefined types, and provides examples of each.

Type

Description

Examples

object

The ultimate base type of all other types

object o = new Stack();

string

String type; a string is a sequence of Unicode characters

string s = "Hello";

sbyte

8-bit signed integral type

sbyte val = 12;

short

16-bit signed integral type

short val = 12;

int

32-bit signed integral type

int val = 12;

long

64-bit signed integral type

long val1 = 12;
long val2 = 34L;

byte

8-bit unsigned integral type

byte val1 = 12;
byte val2 = 34U;

ushort

16-bit unsigned integral type

ushort val1 = 12;
ushort val2 = 34U;

uint

32-bit unsigned integral type

uint val1 = 12;
uint val2 = 34U;

ulong

64-bit unsigned integral type

ulong val1 = 12;
ulong val2 = 34U;
ulong val3 = 56L;
ulong val4 = 78UL;

float

Single-precision floating point type

float value = 1.23F;

double

Double-precision floating point type

double val1 = 1.23
double val2 = 4.56D;

bool

Boolean type; a bool value is either true or false

bool value = true;

char

Character type; a char value is a Unicode character

char value = 'h';

decimal

Precise decimal type with 28 significant digits

decimal value = 1.23M;

Each of the predefined types is shorthand for a system-provided type. For example, the keyword int is shorthand for a struct named System.Int32. The two names can be used interchangeably, though it is considered good style to use the keyword rather than the complete system type name.

Predefined value types such as int are treated specially in a few ways but are for the most part treated exactly like other structs. The special treatment the these types includes literal support and efficient code generation. C#’s operator overloading feature enables developers to define types that behave like the predefined value types. For instance, a Digit struct that supports the same mathematical operations as the predefined integral types, and that conversion to and from these types.

using System;

struct Digit
{...}

class Test
{
static void TestInt() {
int a = 1;
int b = 2;
int c = a + b;
Console.WriteLine(c);
}

static void TestDigit() {
Digit a = (Digit) 1;
Digit b = (Digit) 2;
Digit c = a + b;
Console.WriteLine(c);
}

static void Main() {
TestInt();
TestDigit();

}
}

No comments: