Export (0) Print
Expand All

Built-in Data Types

Visual Studio 2008

C# is a strongly-typed language. Before a value can be stored in a variable, the type of the variable must be specified, as in the following examples:

int a = 1;
string s = "Hello";
XmlDocument tempDocument = new XmlDocument();

Note that the type must be specified both for simple, built-in types such as an int, and for complex or custom types such as XmlDocument.

C# includes support for the following built-in data types:

Data Type

Range

byte

0 .. 255

sbyte

-128 .. 127

short

-32,768 .. 32,767

ushort

0 .. 65,535

int

-2,147,483,648 .. 2,147,483,647

uint

0 .. 4,294,967,295

long

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

ulong

0 .. 18,446,744,073,709,551,615

float

-3.402823e38 .. 3.402823e38

double

-1.79769313486232e308 .. 1.79769313486232e308

decimal

-79228162514264337593543950335 .. 79228162514264337593543950335

char

A Unicode character.

string

A string of Unicode characters.

bool

True or False.

object

An object.

These data type names are aliases for predefined types in the System namespace. They are listed in the section Built-In Types Table (C# Reference). All these types, with the exception of object and string, are value types. For more information, see Value and Reference Types.

Built-in data types are used within a C# program in several ways.

As variables:

int answer = 42;
string greeting = "Hello, World!";

As constants:

const int speedLimit = 55;
const double pi = 3.14159265358979323846264338327950;

As return values and parameters:

long CalculateSum(int a, int b)
{
    long result = a + b;
    return result;
}

To define your own data types, use Classes, Enumerations or Structs.

Converting between data types can be done implicitly, in which the conversion is done automatically by the compiler, or explicitly using a cast, in which the programmer forces the conversion, and assumes the risk of losing information.

For example:

int i = 0;
double d = 0;

i = 10;
d = i;        // An implicit conversion

d = 3.5;
i = (int) d;  // An explicit conversion, or "cast"

Community Additions

ADD
Show:
© 2014 Microsoft