Data types in C#

What is a Data type?

A data type specifies the type of value that can be stored in a variable. C# is a strongly typed language. It means every variable in c# must have a data type specified.

Type of Data Types.

In C#, there are basically 2 types of data types
1)Value type - These are simple types like int,bool,char, float etc
2)Reference type - These are complex types which includes class,arrays, interface and delegates etc

For example 
int age=12; 
float number=12.3;
bool IsPresent=false;
char type='A'; 
string msg="Hello";

Data types and it's range.

This table lists some predefined data types in c# and the range of values it can store.

Signed Data types


Data Type Description Size
sbyte 8-bit signed integer -128 to 127
short 16 bit signed -32768 to 32767
int 32 bit signed -2,147,483,648 to 2,147,483,647
long 64 bit signed -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Unsigned Data types


Data Type Description Size
byte 8-bit unsigned integer 0 to 255
ushort 16 bit unsigned 0 to 65535
uint 32 bit unsigned 0 to 4,294,967,295
ulong 64 bit unsigned 0 to 18,446,744,073,709,551,615


Other Data types


Data Type Description Size
float 32-bit Single-precision floating point type -3.402823e38 to 3.402823e38
double 64-bit double-precision floating point type -1.79769313486232e308 to 1.79769313486232e308
decimal 128-bit decimal type for financial and monetary calculations (+ or -)1.0 x 10e-28 to 7.9 x 10e28
char 16-bit single Unicode character Any valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode)
bool 8-bit logical true/false value True or False
object Base type of all other types.
string A sequence of Unicode characters
DateTime Represents date and time 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999



Each Data type has a size except string and object which states range of values that can be stored in that type. If the value given to this types exceeds the defined range, the compiler throws an error. -


The data types above are alias to the .NET type i.e CLR class name. Below table shows the .NET type and it's alias.

Alias .NET type
byte System.Byte
sbyte System.SByte
int System.Int32
uint System.UInt32
short System.Int16
ushort System.UInt16
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
char System.CHar
bool System.Boolean
object System.Object
string System.String
decimal System.Decimal
DateTime System.DateTime


Data type Conversion

The value of one data type can be converted to another data type.There are 2 types of data conversion.

1) Implicit - The values of some data types are automatically converted to another type. This conversion is done by compiler implicitly. This is called as Implicit conversion.

2) Explicit - For some data types, the values are not automatically converted to another data type. This conversion has to be done explicitly. This is called as explicit conversion.

For example
short s=23;
int i=s;

This data type conversion automatically happends. Because the short data type is smaller than int data type.

Conversion from one data type to other for types like long to int or long to double cannot happen explicitly. So in this case, an explicit conversion has to be done.

For example
long l=23;
ulong ul=(ulong) l;
Console.WriteLine(ul);


Find .NET type of data type

you can find the .NET type of particular data type using the GetType() method.

int age=34;
Console.WriteLine("Type is " + age.GetType());

Output

Type is System.Int32


Minimum and Maximum value

You can find out the minimum and maximum value that the data type can store using the2 properties. MinValue and MaxValue.


Console.WriteLine(byte.MinValue);
Console.WriteLine(byte.MaxValue);

Output

0
255

Comments

Popular posts from this blog

Implicitly typed keyword - var in C#

Classes and objects in C#