Different Data types in C#

Data Type in C#


In C# data type has been categorized in three types. these are below: -

  • Value Type
  • Reference Type
  • Pointer Types

Below is an overview of different data types exist in C# language

Value Type


In C# value type are those data types which stored into the stack. Stack is a memory type in our local system which allow the space for Value type variables. In value type variable data can assign directly. In C# value type have been defined under System.ValueType namespace.

Predefined Value Data type of C# are below



Let use discuss about all types one by one in details


Integer

An integer data type in C# has been categorized into three types. Integer type take 4 byte space for memory allocation for defined type.

    
int number1;
uint unsignedint;

Using this way we can define an integer value in our program. The default value of an integer value is 0. An integer type variable can hold value -2147483648 to 2147483647.

An unsigned integer value can store from 0 to 4294967295.


Long

An long data type in C# define as show in below. Long type takes 8 bytes space for memory allocation for defined type. Long have two types on is signed or simple long type and another is unsigned long type.


    long num1;
    ulong num2u;

An signed long type hold value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

An unsigned long type hold value from 0 to 18,446,744,073,709,551,615.

Default value of long type is 0L.


Float

An float data type in C# define as shown in below. Float types takes 4 byte space for memory allocation for defined types.

   float num;

A float type variable can hold value from -3.4 x 1038 to + 3.4 x 1038  till seven precision. Default value of float type is 0.0F


Double

An double data type in C# define as shown in below. Float types takes 8 byte space for memory allocation for defined types.

   double num;

A float type variable can hold value from 5.0 * 10 -324  to 1.7*10 308 till 15 digits of precision. Default value of double type is 0.0D.


Float

An float data type in C# define as shown in below. Float types takes 4 byte space for memory allocation for defined types.

   float num;

A float type variable can hold value from -3.4 x 1038 to + 3.4 x 1038  till seven precision. Default value of float type is 0.0F


Decimal

An decimal data type in C# define as shown in below. Decimal types takes 8 byte space for memory allocation for defined types.

   decimal num;

A float type variable can hold value from -7.9 x 1028 to 7.9 x 1028. Default value of decimal type is 0.0M.


Char

An char data type in C# define as shown in below. Char types takes 1 byte space for memory allocation for defined types. It store 16 bit unicode character.

   char val;

A char type variable can hold value from -128 to 127. Default value of char type is '\0'. '\0' denotes the sign of end of character. Char have also two types i.e. signed/ normal char type and another is unsigned char type.


Bool

An bool data type in C# define as shown in below. Bool types takes 1 bit space for memory allocation for defined types.

bool val;

A bool type variable can hold value either 0 or 1. 0 denotes false and 1 denotes true. Default value of bool type is 0.


Byte

An byte data type in C# define as shown in below. Byte types takes 1 byte space for memory allocation for defined types. It hold value 8 bit unsigned integer value which range start from 0 to 255. Its default value is 0.


Reference Type Data in C#


What is Reference Data Type ?

A reference type data does not contain the actual data which stored in a variable, they contain a reference of the variables. It keep the address of the variable.

In C# there two main Reference types available

    • Predefined Type      :: Object Type, String Type; 
    • User Defined Type   :: Class Type, Interface Type

1st Predefined Reference Type

a) Object


An object type in C# is the base class of all data types. An Object reference to the System.Object namespace. An Object type can contain any type of variable such Value type, reference type or an object since Object define at the top label of all types. 

If we assign any other type value in an object it need conversion before assigning or retrieving value in vise versa. Here is an example first we are assigning an integer type to an object then again getting back from object type:-

int i=10;
Object obj = i; // This process called Boxing

Note :: The process of assigning a value type to an object type called Boxing.

Now we getting back value from that object shown in below

int k;
k = (int) obj; // This process called Unboxing

Note :: The process of assigning a reference type to a value type called Unboxing.


Boxing is Implicit conversion but an Unboxing is a Explicit conversion.

b) String


A string type contains string value. It allows to store string value only. String type data reference from System.String namespace. A string value start with double quote i.e. " and end with double quote. Below is an example we define a string type.

string vlidname;

Example

    string myname = "Information Technology";



c) Pointer


A pointer type have same feature available in C# as available in C or C++. 
A pointer type store/keep address of another type. A pointer type define with an * sign. Here is an example of pointer type :- 

int* intPtr; // It can keep address of integer type.

string* strPtr; // It can keep address of string type.

char* charPtr; // It can keep address of character type.


Some of the other types such as Class Type, Interface Type we will discuss in details in upcoming article...  






Request :: If you like this article Please share this and comments  .....




No comments:

Post a Comment

If you have any doubt please let me know..