Variables and data types in C#.Net

Variables are used to store the data of different types.

Types of variables

In C#.Net, we have mainly 2 types of variables.

  1. Value types like int, float, double
  2. Reference types like string, class objects, delegates, pointers

All classes are reference type. Everything else including struct is of value type. All value types are stored in stack (because we know the memory requirements. So static memory blocks can be used from stack) while all reference types are stored in heap area of memory. (because we do not know how much memory will be required for the object and memory needs to be allocated dynamically for objects)  When you pass value, copy is made. When you pass the object, reference is passed.

Here is the list of value types

  1. bool – used to store either true or false
  2. char – used to store one unicode character
  3. decimal, double, float – used to store all types of numbers (Integers + fractional numbers)
  4. byte, sbyte – used to store bytes
  5. int, long, short, uint, ulong, ushort – used to store integer numbers

The major difference between value type and reference type is that variables of value types store actual values While variables of reference type store the address of the values.

Example – In below statement, we have created a variable called as count and it is initialized with value 0.

int count=0;

Boxing and Unboxing
Boxing – Mechanism of converting value type to the reference type
Unboxing – Mechanism of converting  the reference type to value type
example –
int a = 10;
object myobject = a; //boxing done
int b = (int) myobject; //unboxing done
Converting data type of variables (Type Casting)

We can convert the data type of the variable using 4 ways.

  1. Implicit  – done automatically by compiler
  2. Explicit – Programmer needs to do this type of conversion
  3. Using Convert class
  4. Int32.Parse() and Int32.TryParse()

Below C# program illustrates how to convert the data types of the variables.

using System;
namespace SimpleApplicationOrSolution
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //Below statment is commented as it's illegal in C#
            //static int counter = 0;

            //int is Value type data
            int age = 28;

            //string is a reference type data
            string name = "Rihanna";

            Console.WriteLine("Name is " + name);
            Console.WriteLine("Age is " + age);
            Console.ReadKey();

            //Now let us convert the data type of age from int to string
            string age_string = Convert.ToString(age);
            Console.WriteLine("Age is " + age_string);

            //Another way to convert data type age from int to string
            Console.WriteLine("Age is " + age.ToString());

        }
    }
}

Constant variables

If you want to make any variable read-only, you can use const keyword. For example – Below statement will create constant variable – pi. Please note that we can not modify that variable again.

const float pi = 3.14;

Static Class variables

Class variables can be marked as static. Static class variables are accessed at class level. Please note that in C#.Net, you can not create static variables inside class methods.

Structs and Enums solve 2 different problems. Struct is a data structure while enums are used to replace numbers with identifiers.

You may also like...