web analytics

C# Integral Types

Options

codeling 1599 - 6654
@2016-12-05 15:53:43

The following table shows the sizes and ranges of the integral types, which constitute a subset of simple types.

Type Range Size
sbyte -128 to 127 Signed 8-bit integer
byte 0 to 255 Unsigned 8-bit integer
char U+0000 to U+ffff Unicode 16-bit character
short -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65,535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer

If the value represented by an integer literal exceeds the range of ulong, a compilation error will occur.

@2016-12-06 11:24:36

When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In the following example, it is of the type long because it exceeds the range of uint:

long long1 = 4294967296;  

You can also use the suffix L with the long type like this:

long long2 = 4294967296L;

When you use the suffix L, the type of the literal integer is determined to be either long or ulong according to its size. In the case it is long because it less than the range of ulong.

@2016-12-06 11:30:37

A common use of the suffix is with calling overloaded methods. Consider, for example, the following overloaded methods that use long and int parameters:

public static void SampleMethod(int i) {}

public static void SampleMethod(long l) {}

Using the suffix L guarantees that the correct type is called, for example:

SampleMethod(5); // Calling the method with the int parameter

SampleMethod(5L); // Calling the method with the long parameter
@2016-12-06 11:39:26

Int64 Structure

Int64 represents a 64-bit signed integer.

Int64 is an immutable value type that represents signed integers with values that range from negative 9,223,372,036,854,775,808 (which is represented by the Int64.MinValue constant) through positive 9,223,372,036,854,775,807 (which is represented by the Int64.MaxValue constant. The .NET Framework also includes an unsigned 64-bit integer value type, UInt64, which represents values that range from 0 to 18,446,744,073,709,551,615.

To view the .NET Framework source code for this type, see the Reference Source.

 

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com