Data Types
There are following data types in C#:
- Value type
- Reference type
Value Type
- Value types directly contain data
- Allocated on stack
- It cannot be
null
Data types such as integers, numeric, bytes, Booleans,
char
are called value type. Value types are derived from the ValueType
class in System namespace
.
There are two categories of value types which are built in and user defined.
- Built in value types are
int
,char
,decimal
,float
,long
, etc. and - User defined value types are structures (
DateTime
is a value type, a structure) andenum
.
For example, when we declare the
int a
compiler allocates 4 bytes of memory (32bit). When we give a value such as 7 to the variable, it copies this value to that block of memory. We can declare a data type by writing the data type name like int string
, etc. then write any name of a variable by using name conventioncamelCasing
like maxTemperature
, etc.int a;//declaring variable aa = 7;//initializing aint a=7;//declaring and initializing in one line
Different data types have different sizes like
int
has 4 byte size, we can find the size of any data type through a method called sizeof()
like this:static void Main(string[] args)
{
Console.Write("size of int:"+
sizeof(int));//you can replace int with any data type you want to check Console.ReadKey();
}
Note: You can check only the sizes of value type like
int
, float
, long
, etc. but you cannot check the sizes of reference types like string
object.
Value types in C# are given below:
- Integral:
sbyte
,byte
,short
,ushort
,int
,uint
,long
,ulong
, andchar
- Floating
point
:float
,double
- Decimal:
decimal
Booleans:
true
or false
values,
Nullable : Nullable
data typesNullable Types and How to Declare nullable Types?
You can assign
null
values to data types in C# that can hold null
and values as well. Note that var
does not support nullable types.
Nullable types are declared by putting a question mark after data type and then make it equal to
null
like below:<data type>? <variable name>=null; //syntaxInt ? a=null; //assigning nullInt ? a=55; //assigning null and a valueVar? a=55 //it will give error
There are two properties for checking the value in nullable types:
HasValue()
it will returntrue
if there is value otherwisefalse
andValue()
will return the actual value of variable. Like in the below example:
Reference Type
Reference type does not contain any data directly but it contains a reference or an address to a memory location where the actual data is stored. In other words, it is only a pointer to a memory location.
- Reference types indirectly contain data
- Allocated on heap and reference on stack
- it can be
null
There are two categories of reference types built in and user-defined:
- Built in reference types are
object
,string
, anddynamic
. - The user-defined reference types are
class
,interface
, ordelegate
.
Reference is given on stack and value is stored in a memory called heap as below in the figure:
Example
test obj; //allocating reference on stackobj= new test(55);//allocating object on heap
Multiple reference type can point to a single memory location as shown in the figure below:
Data Type Categories
Copying Value Type and Reference Type
Copying Value Type
When we assign a value type to another value type, it creates a different copy on the same memory (stack).
As you can see in the following diagram, when we copy
int a
tocopya
,
it hold the same value but on memory there are two different block of copies on stack, one for int a
and one forint copya
. If we modify the value of int a
value ofint copya
does not change. It's most often called as static
memory (heap).
Have a look at the following code:
int a = 55;//declare a and initializeint copya = a;//copya contains the copy of value a
When first line runs, it stores a value in stack, then second line runs it stores value on top of previous value as shown in the above diagram.
Copying Reference Type
When we assign a reference type to another reference type, it creates two references on stack pointing to the same object on heap. Multiple references can point to the same object.
For example, when we create a
test
class object, it creates a reference obj
on stack when we copy this reference into objcopy
it creates second reference pointing to the same memory on heap. If we change one object, the other is also affected. Heap is a dynamic memory.
As you can see in the diagram:
Conversion Conversion Emoticon Emoticon