- Back to Home »
- C#.NET »
- C#.NET Datatypes
Posted by :
Sudhir Chekuri
Wednesday, 30 December 2015
C# value datatypes
In value type we will assign value directly to the variable.There are many value datatypes. Each type will store a specific type of value within its range.
Here we have a list of value datatypes with its memory in bytes.
This list of C# datatypes contain numeric datatypes like byte, short, int, long, sbyte, ushort, uint, ulong, float, double, decimal and text datatypes like char, string.
byte, short datatypes are used to store small numbers with less range.
int datatype is used to store a number within a range of 10 digit. If you want to store number more than that you have to use long.
float datatype is used to store number with decimal point. If you want to store a float value with higher range then you can use double and decimal.
char datatype is used to store a single character. The value of char datatype should be surrounded by a single quote.
String datatype is used to store multiple characters in a variable. You can store words or sentences using string datatype.
bool datatype is used to store only true or false.
Memory for variables of value type is created in stack memory.
Performance will be more when you work with value type variables.
Datatypes memory
Datatypes - memory in bytes - Range
byte 1 0 to 255
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647
long 8 (-7.9 x 1028 to 7.9 x 1028) / 100 to 28
sbyte 1 -128 to 127
ushort 2 0 to 65,535
uint 4 0 to 4,294,967,295
ulong 4 0 to 18,446,744,073,709,551,615
float 4 -3.4 x 1038 to + 3.4 x 1038
double 8 (+/-)5.0 x 10-324 to (+/-)1.7 x 10^308
decimal 16 (+/-)5.0 x 10-324 to (+/-)1.7 x 10^308
bool 1 true or false
char 2 U +0000 to U +ffff
string 20+
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647
long 8 (-7.9 x 1028 to 7.9 x 1028) / 100 to 28
sbyte 1 -128 to 127
ushort 2 0 to 65,535
uint 4 0 to 4,294,967,295
ulong 4 0 to 18,446,744,073,709,551,615
float 4 -3.4 x 1038 to + 3.4 x 1038
double 8 (+/-)5.0 x 10-324 to (+/-)1.7 x 10^308
decimal 16 (+/-)5.0 x 10-324 to (+/-)1.7 x 10^308
bool 1 true or false
char 2 U +0000 to U +ffff
string 20+
Examples for creating variables with different value types
int i=10;
char c='c';
string s="abcd";
float f=123.34f;
bool b=true;
So, as shown in the above example we have to follow the below rules in c#.
No need of quotes around the numeric value.
C# char datatype value should be surrounded by single quotes.
C# string datatype value should be surrounded by double quotes.
C# bool datatype value will accept only true or false.
C# Reference type
C# Object type
Object is a reference type which is used to store any type of value inside it. You can store numeric values , decimal values , characters , strings or bool values inside it.
Here the memory address is stored in object variable.
Values of object type will be stored in heap memory.
Example for creating variables with object type
object o = 12;
object o1 = 'a';
object o2 = "abc";
Here the memory address is stored in object variable.
Values of object type will be stored in heap memory.
Example for creating variables with object type
object o = 12;
object o1 = 'a';
object o2 = "abc";
C# Dynamic type
You can store any type of value in dynamic type also. In this datatype of variable is recognized in run time.
Example for creating variables with dynamic type
dynamic d = 123;
dynamic d1 = 'a';
dynamic d2 = "abc";