Data Types

Data Types tell the C# Compiler what kind of value can a variable hold. In C# we have many data types like int string, char and so on which could be grouped into 3 categories

Data Types tell the C# Compiler what kind of value can a variable hold. In C# we have many data types like int string, char and so on which could be grouped into 3 categories of data type:

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

Value Type:

We can assign the values directly to them.
Derived from: System.ValueType

SnoTypeDefault Value
1boolFALSE
2byte0
3char‘\0’
4decimal0.0M
5double0.0D
6float0.0F
7int0
8long0L
9sbyte0
10short0
11uint0
12ulong0
13ushort0

Reference Type:

They contain the reference to the variable and they don’t store the actual value. In short, they refer to a memory location.
Object, dynamic, and string are some of the built-in reference type.

Object Type: It is the base class for all the data types in C#, so it can store any type of value.
When a value is converted from a value type to an object type it is called BOXING and if the object type is converted to a value type it is called UNBOXING

Object objectname;
objectname = 500; //This is Boxing, 500 is a value i.e being converted to object type

Dynamic Type: You can store any value in the dynamic type variable. Type-checking for these variable type is done at runtime. Think of it as a variable that doesn’t require typecasting.

// dynamic <variable_name> = value; //Syntax

using System;

namespace Csharp_ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic universaltype;
            universaltype = 100;
            Console.WriteLine("Variable Value: {0} and its type {1}", universaltype, universaltype.GetType().ToString());

            universaltype = "Hello World!";
            Console.WriteLine("Variable Value: {0} and its type {1}", universaltype, universaltype.GetType().ToString());

            universaltype = true;
            Console.WriteLine("Variable Value: {0} and its type {1}", universaltype, universaltype.GetType().ToString());

            universaltype = DateTime.Now;
            Console.WriteLine("Variable Value: {0} and its type {1}", universaltype, universaltype.GetType().ToString());

            Console.ReadKey();
        }
    }
}

Are you wondering if dynamic variables are so amazing why isn’t it being used more in our day to day coding?
Well, dynamic variables are good to be used during handling of complex code logic and cleaning dirty legacy code where variable reuse will reduce complexity.

Other than that using it everywhere will cause unnecessary code complexity and increase chances of bugs in your application. The thing that makes dynamic fascinating is the one that will cause issues i.e No Type Checking during CompileTime.

See the code below: This will throw RuntimeBinderException error i.e Method DoNothing() is not defined.

using System;

namespace Csharp_ConsoleApplication
{
    public class Student
    {
        public int Studentid { get; set; }
        public string StudentName { get; set; }
        public bool StudentAttendance { get; set; }

        public void DisplayStudentDetail()
        {
            Console.WriteLine("Student id: {0}", Studentid);
            Console.WriteLine("Student Name: {0}", StudentName);
            Console.WriteLine("Student Attendance: {0}", StudentAttendance);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            dynamic StudentVariable = new Student();

            StudentVariable.Studentid = 1;
            StudentVariable.StudentName = "Tejaswi";
            StudentVariable.StudentAttendance = false;

            StudentVariable.DisplayStudentDetail();

            StudentVariable.DoNothing();

            Console.ReadKey();
        }
    }
}

Like Dynamic we have a another variable called var.

VAR: It is an implicit typed local variable i.e compiler will fetch the correct data type based on the value to the right of the “=” operator.

Implicitly-typed variables must be initialized at the time of declaration, otherwise C# compiler would give an error: Implicitly-typed variables must be initialized.

using System;

namespace Csharp_ConsoleApplication
{

    class Program
    {
        static void Main(string[] args)
        {
            var i; //not valid you need to assign value at the time of declaration
            var varvariable = 100; //valid
            varvariable = 200;
            Console.WriteLine("Variable Value: {0} and its type {1}", varvariable, varvariable.GetType().ToString());

            //varvariable = "Hello World!" "This will throw an error as typecasting is done at the compile Time

            var str = "Hello World!";
            Console.WriteLine("Variable Value: {0} and its type {1}", str, str.GetType().ToString());

            var boolean = true;
            Console.WriteLine("Variable Value: {0} and its type {1}", boolean, boolean.GetType().ToString());

            var CurrentDate = DateTime.Now;
            Console.WriteLine("Variable Value: {0} and its type {1}", CurrentDate, CurrentDate.GetType().ToString());

            Console.ReadKey();
        }
    }
}

String: It allows you to assign any string value to a variable.

String str = "TejaXSpace";

Pointer Type:

They store the memory address of another type.

datatype* <identifier>; //Syntax

int* ptr;

Leave a Reply

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