Data type or variable are an important thing in any programming language. It is the basic starting point for a programmer. It generally tells to the compiler the type of value it can hold.

Data types in C# :

C# is typically a typed language, which means that variables must have a specific data type assigned to them at compile-time.  Let’s take an example.

In above example we have declared a string variable in C# as String s, it tells the compiler that string is a data type that holds a text value. Similarly in below example.

Here, we have declared an integer type in C#, it tells the compiler that variable i is of type int that will be used to hold numbers.

Data Type Overflow:

Data types has some specific size and length. If we declare a type and at run time if the value, it holds exceeds the max limit that the type can hold then we get the data overflow issues. So its very important to have proper data types in our program.

In below example.

  • We have declared a type of an int and assigned max integer value to it.
  • During initialization  i holds value as 2147483647 (max of integer value) .
  • We have another variable declared as k, this data type assigned the value of  2147483647 * 100 at run time.

Data Type Overflow

Figure – 1 – Data Type Overflow

In the above example,

  • We are trying to assign variable k to hold a value 214748364700 which is outside of the upper limit of int at run-time. hence the compiler throws “Overflow Exceptionat run time.

Data Type in C# details :

C# offers multiple built-in data types to represent different kinds of values, below table lists the data types available in C# along with their range and size in byte.

Data type Size(in byte) Range Description
byte 1 0 to 255 Unsigned 8-bit
sbyte 1 -128 to 127 Signed 8-bit
short 2 -32,768 to 32,767 Signed 16-bit
ushort 2 0 to 65,535 Unsigned 16-bit
int 4 -2,147,483,648 to 2,147,483,647 Signed 32-bit
uint 4 0 to 4294967295 Unsigned 32-bit
long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit
ulong 8 0 to 18,446,744,073,709,551,615 Unsigned 64-bit
float 4 -3.402823e38 to 3.402823e38 32-bit floating point
double 8 -1.79769313486232e308 to 1.79769313486232e308 64-bit floating point
object 4 byte address Base type for all types
char 2 Unicode characters 16-bit Unicode character
string 4 byte address Array of character
decimal 24 (+ or -)1.0 x 10e-28 to 7.9 x 10e28 128-bit floating value
bool 1 True, False true/false value
DateTime 0:00:00am 1/1/01 to
11:59:59pm 12/31/9999
Represents date and time value
Dynamic 4 byte address Identical to object

All types in C#.Net are derived directly or indirectly from System.Object. So by default all data types in C#.Net supports the methods that are present in System.Object.

Below methods are by default derived in all data types.

  • Equals – Determines if two objects are equal.
  • GetHashCode – Returns default hash function.
  • ToString – Returns a string that represents current object.
  • GetType – Returns current type.

In the above example, data type string and int has the default methods as derived from System.Object.