C# Data type

In the previous post, we have explained about C# variables, now C# variables can be categorrized into 3 categories

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

Let's understand this one by one.

Value type

Value type variables are those, which can be assigned a value directly. These are the built-in primitive data types, such as char, int, and float, as well as user-defined types declared with struct. They are derived from the class System.ValueType.

When you declare an int type variable, the system allocates memory to store the value. In the previous tutorial, you can check the table which shows the memory  alloted to int type = 4 .

To get the exact size of a type or a variable on a particular platform, you can use the sizeof method. The expression sizeof(type) yields the storage size of the object or type in bytes.

using System;

namespace DataTypeCSharp {
   public class Program {
     public static void Main(string[] args) {
         Console.WriteLine("Size of int: {0}", sizeof(int));


Size of int: 4

Refernce type

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

Classes and other complex data types that are constructed from the primitive types. Variables of such types do not contain an instance of the type, but merely a reference to an instance

In other words, they refer to a memory location. Using more than one variable, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built in reference types are:object, dynamic and string.

Object Type in C#

The Object Type is the base class for all data types in C#, .NET Framework. Object is an alias for System.Object class, it is present in the System namespace.Every class in C# is directly or indirectly derived from the Object class. If a Class does not extend any other class then it is the direct child class of Object class and if extends other class then it is an indirectly derived.

So object types can be assigned values of any other types, value types, reference, types, predefined or user-defined types. However, before assigning values, it needs type conversion.

When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type it is called unboxing.

object obj;
obj = 50; // this is boxing

Dynamic type in C#

In dynamic type variable, you are free to store any type of value in it in C# programming.Type checking for these types of variables takes place at run-time.

Here is the general form to declare a dynamic type in C# programming:

dynamic x = 20;

so basically, syntax of dynamic type variables is like

dynamic {variable_name}= {value};

String type in C#

String type is one of the most widely used data types. It allows you to assign the string values ( series of characters value) to a variable.

The string keyword refers to the object type of the System. String class

String str = "C# Datatype Tutorial";

Pointer type in C#

A pointer type variable store the memory address of another type. Pointers in C# have the same capabilities as in C or C++.

Here is , how to decalre pointer in C#

type *identifier;


char *chpt;
int *intpt;

Share Tweet