C# Data Type In Detail

12:37
Every language has data type to work with different values. C# is strongly type language. Now what does this term means? Well strongly type means which data you have assign to the variable it will not store other data type in it. Example:

int a=10;
byte=a;


The above statement will give you an error. Since int store the data in decimal format have 4 byte and bye store numbers up to 0 to 255. So it will not automatically convert an int to byte. This is called implicit type conversion .While you can do reverse. On the other hand we have sometime tell compiler explicitly to convert object type to value type. This is called explicit type conversion. Let me explain with you laymen example:

Consider int as 2 liter bottle and byte as 1 liter bottle. 2 liter bottle can handle 1 liter water. While you reverse is not true that is 1 little bottle cannot handle 2 liter water. It will overflow. The same case with int and byte. Int (Integer) is 2 liter bottle and byte is 1 liter.

The variables in C# are categorized under 3 Heads:


  1. Value Type
  2. Reference Type
  3. Pointer Type
Value Type:

Value type is derived from System class: System.VlaueType; these variables can assign the values directly i.e. variable directly contain data. Some example of such type are floating point number, integer, Char, double, Boolean etc. Once they are declared then memory is allocated directly. Here is all details that you need to know about.


Type
Representation and Memory
Example
bool
Boolean Value
Bool flag=true;
byte
8 bit unsigned integer (0-255 only)
Byte b=10;                
char
16 Bit Unicode character
char latter=’a’;
int
32 bit sign integer type
int a=25000;
float
32-bit single-precision floating point type
float f=12.16;
double
64-bit double-precision floating point type
double d=12.154780;
long
64-bit signed integer type
Long int =150000;
sbyte
8-bit signed integer type(-128-127)
sbyte a=25;
short
16-bit signed integer type
short int=2500;

You can check the size of every data type by using size of method. This method returns size of datatype. Here is an example code:

using System;
namespace DataTypeSize{
   class CheckSizeOf
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Size of int: {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}
Here you can use float double or any other data type specified above. And Output will provide you storage size for datatype in byte.

Reference type:
Reference does not store any value but they store the reference for the variable. In short they store references of memory location of variable. If data at that location change by any variable then it will automatically get reflected in reference variable. Following are built in reference types:
  • Object
  • Dynamic 
  • String
Object:
Object is main base class for all the data type classes in C#. Alice for object class is System.Object. It can store all type of data in it. Befre assigning the value it need to get converted. Example:
using System;
namespace DataTypeConversion
{
   class TypeConversion
   {
      static void Main(string[] args)
      {
         Object o=100;// Here int is assign to Object instance o.
         Console.WriteLine(0);
      }
   }
}

In above example object type is storing data of integer type. This is automatically getting done. Since object is ultimate class for all data type. While doing reverse you need to do explicate type conversion. This is call as boxing. Example: 

using System;
namespace DataTypeConversion
{
   class TypeConversion
   {
      static void Main(string[] args)
      {
         object o=100;
         int a=Convert.ToInt32(o);
         Console.WriteLine(a);
      }
   }
}
Here we have used here Convert.ToInt32() method that take one argument for converting data type to Integer format. This called unboxing.

String Type:
String type is used to store string or can say alphanumeric character. String is alias for System.String; String type has also derived from Object type.
Here is an example:

String s="Hello How are you?";

Pointer Type:
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.
Example :

Char* cprt;
Int* ap;

There are some other data types also available in C# but like class interface structure delegates that we will discuss in separate section all together. So we have seen almost all normally used data types. In Next chapter we will see further.

Here we have seen Value type, reference type and pointer type in details. Also we have seen their usage for the same. Stay tuned for more.

Share this

Related Posts

Previous
Next Post »