logo search
CSharp_Prog_Guide

Общие сведения о типах данных

Тип можно описать как:

Типы также могут быть заданы как:

Casting

Converting between data types can be done explicitly by using a cast, but in some cases, implicit conversions are allowed. For example:

static void TestCasting()

{

int i = 10;

float f = 0;

f = i; // An implicit conversion, no data will be lost.

f = 0.5F;

i = (int)f; // An explicit conversion. Information will be lost.

}

A cast explicitly invokes the conversion operator from one type to another. The cast fail if no such conversion operator is defined. A cast operation between reference types does not change the type of the underlying object; it only changes the type of the variable that is being used as a reference to that object.

You can write custom conversion operators to convert between user-defined types.

Example

The following program casts a double to an int. The program will not compile without the cast.

class Test

{

static void Main()

{

double x = 1234.7;

int a;

a = (int)x; // cast double to int

System.Console.WriteLine(a);

}

}

Output

1234