Home > C# > Data types in c#

Different Data types are available in C#. We can divide the Data types in C# in different categories.

What are Different Data Types in C#?

Different data types in C# includes:

  1. Value types
  2. Reference types
  3. Pointer types

Value types

These are those variables that store values directly. System class derives these variable.ValueType.
Data types included in these are:

  • Bool
  • Byte
  • Char
  • Decimal
  • Double
  • Float
  • Int
  • Long
  • Short

To obtain the actual size that a data type can store we can use sizeof function, which returns us the size of an object passed to it as a parameter in bytes.
Example program:

using System;

namespace data_types
{
     class Program
     {
          static void Main(string[] args)
          {
               // size of integer  
               Console.WriteLine("size of int = {0}", sizeof(int));
               // size of char  
               Console.WriteLine("size of char = {0}", sizeof(char));
               // size of float  
               Console.WriteLine("size of float = {0}", sizeof(float));
               // size of bool  
               Console.WriteLine("size of boolean = {0}", sizeof(bool));
               // size of long  
               Console.WriteLine("size of long = {0}", sizeof(long));
               // size of short  
               Console.WriteLine("size of short = {0}", sizeof(short));
          }
     }
}

Output

Different Data Types in C#

Reference Types

Reference types are those variables that do not store values directly. Rather they store address or memory location.
These include:

  • Dynamic
  • Object
  • String

Dynamic types
Dynamic type can store the value of any type. In it, type checking is done at runtime. i.e. when the value is given.
Syntax to create a variable with dynamic data type is:

     dynamic variable_name;

dynamic is used to create a variable which can store the value of any type.
E.g.

     dynamic x= 10;
     dynamic y=11.6;

Example Program

using System;

namespace datatypes
{
     class Program
     {
          static void Main(string[] args)
          {
               dynamic d = 20;
               Console.Write("Dynamic Value :"+d+"\n");
               d = "Test";
               Console.Write("Dynamic Value :" + d + "\n");
               d = 30.5;
               Console.Write("Dynamic Value :" + d + "\n");
          }
     }
}

Output
dynamic type

Object
Object types can be assigned value of any predefined or user-defined type. However, type conversion is done before assigning a value to it. This type conversion is called Boxing. While when an object type is converted to value type it is called as UnBoxing.

Example Program

using System;

namespace datatypes
{
     class Program
     {
          static void Main(string[] args)
          {
               object obj;
               obj = 150;  // this is boxing  
               Console.Write("Object Value :"+obj+"\n");
          }
     }
}

Output
object type

String
This type allows a user to store any string in a variable. A string variable can contain characters, numbers, special characters like -,_,:, etc.
E.g.

string a= “this is a string”;
sting b= "this_is_a_string_vriable : ";

Example Program

using System;

namespace datatypes
{
     class Program
     {
          static void Main(string[] args)
          {
               string str = "this is a string";
               Console.Write("String Value :" + str + "\n");
          }
     }
}

Output
string type

Pointer types

Pointer types variables store memory address of another data type. These are same as pointers in C++.

type * variable-name;

E.g.

int x =10;
int *y;
y= &x;

Example Program

using System;

namespace datatypes
{
     class pExample
     {
          public unsafe void Pointer_Example()
          {
               int x = 10;
               int* y;
               y = &x;
               Console.Write("Pointer Value :" + *y + "\n");
          }
     }
     class Program
     {        
          static void Main(string[] args)
          {
               pExample p =  new pExample();
               p.Pointer_Example();
          }
     }
}

Output
pointer type

Note: -If you are facing an error while using the unsafe keyword in pointer type then you can visit here to solve this issue.

Leave a Reply

Your email address will not be published. Required fields are marked *