O'Reilly logo

C# 3.0 Pocket Reference, 2nd Edition by Joseph Albahari, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Numeric Types

C# has the following predefined numeric types.

C# type

System type

Suffix

Size

Range

Integral—signed

   

sbyte

SByte

 

8 bits

–27 to 27–1

short

Int16

 

16 bits

–215 to 215–1

int

Int32

 

32 bits

–231 to 231–1

long

Int64

L

64 bits

–263 to 263–1

Integral—unsigned

   

byte

Byte

 

8 bits

0 to 28–1

ushort

UInt16

 

16 bits

0 to 216–1

uint

UInt32

U

32 bits

0 to 232–1

ulong

UInt64

UL

64 bits

0 to 264–1

Real

    

float

Single

F

32 bits

±( ~10–45 to 1038)

double

Double

D

64 bits

±( ~10–324 to 10308)

decimal

Decimal

M

128 bits

±( ~10–28 to 1028)

Of the integral types, int and long are first-class citizens and C# and the runtime favor both. The other integral types are typically used for interoperability or when space efficiency is paramount.

Of the real number types, float and double are called floating-point types and are typically used for scientific calculations. The decimal type is typically used for financial calculations, where base-10-accurate arithmetic and high precision are required.[1]

Numeric Literals

Integral literals can use decimal or hexadecimal notation; hexadecimal is denoted with the 0x; prefix. For example:

	int x = 127;
	long y = 0x7F;

Real literals can use decimal and/or exponential notation. For example:

	double d = 1.5;
	double million = 1E06;

Numeric literal type inference

By default, the compiler infers a numeric ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required