Breaking News

Primitive Data Types in C#

The built-in data type in C# is called primitive data types. Here we are giving you the range of most commonly used primitive data types in C#.
 
Data Type Description Size (bits) Range
int Whole numbers 32 <->‡231 through 231 <-> 1
long Whole numbers 64 <-> §263 through 263 <-> 1
Float Floating-point numbers 32 ± 3.4*1038
double Double precision floating point numbers 64 ± 1.7*10308
decimal Monetary Values 128 28 significant figures
string Sequence of Characters 16 bits per character Not Applicable
char Single Character 16 0 through ~216 < – > 1
bool Boolean 8 true or false
byte Unsigned integer 8
sbyte Signed integer 8
short Signed integer 16
uint Unsigned integer 32
ulong Unsigned integer 64
ushort Unsigned integer 16
 
Notes: ~ The value of 216 is 32,768;
‡ The value of 231 is 2,147,483,648;
§ The value of 263 is 9,223,372,036,854,775,808;
 
The Integer Type
 
The C# integer type break into two types – signed and unsigned Signed value type has contained positive and negative values while unsigned value type has contained only positive values.
 
The sbyte and byte type
 
Both sbyte and byte type represent a single byte or character. Bytes can be use to store very small numbers, such as day of the month. The byte values are to be used for flags, single values, and object serialization. C# has no true concept of pointers, so bytes are often used to index into a string and write it out.
 
Note: A byte value is restricted to only 8 bits of precision; a long string could overflow the index.
 
The short value
 
In real application, the short value is used to represent small integers. The signed short values can have the range between +32767 to -32767 with zero. The unsigned short values can have the range from 0 to 65535.
 
The Floating-Point Type
 
In C#, floating point numbers break into two types– single and double floats. Single floats can hold the values up to 1038, the double floats can hold the values up to 10308.
 
The Boolean and Decimal Type
 
The other two important value types are Boolean type and Decimal type. Boolean are simply contains true or false values. In C#, it can’t be assigned to any other type and it can’t be used in any sort of computation. The result of an expression in C# is not a Boolean expression, unless you specifically assign it as true or false. The given example doesn’t work in C# because you can use Boolean values in only Boolean expression:
 
int a=5;
if(x){ }
 
for the above example, you must write like this:
 
int x=5;
if(x!=0){}
 
These two code pieces are functionally equal in C++ and Java, but don’t do the same in C#.
 
The Decimal data type, is equivalent to a long integer that behaves as if it were a floating –point number. Decimal values are intended to be used for economic computation or other calculations in which transaction beyond the decimal point is not desired behavior.
 
Default values of datatype
 
Value Type Default Value
bool False
byte 0
char ‘\0’
decimal 0.0M
double 0.0D
enum The value of the expression (E)0 (where E is the enum identifier)
float 0.0F
int 0
long 0L
sbyte 0
short 0
struct The value produced by setting all value-type fields to their default values and all reference-type fields to null
uint 0
ulong 0
ushort 0
 
 

Check Also

Exceptions In C#

System.Exception is the base class for all other exceptions. Below this level are the System.ApplicationException …