Var in C# and Dynamic in C#

These keywords, var in C# and dynamic in C#. As you know we need to use some predefined keywords to declare a variable such as an int, string, float, double etcetera. But there are some more keywords that are special and are used in a special situation.

Var in C# and Dynamic in C#

var in C# and dynamic in C#

You can use int, string, float, double etcetera when you know what type of data going to be stored in the variable. What if you don’t know what type of data will be coming and being held in the variable. Such situations might occur when the data is coming from an API.

In such situations, you can use Var or Dynamic keywords to declare a variable. The variable declared by var or Dynamic in C# can be changed in the runtime and compiled time. The variable will change its type of int if the data saved in it is int, if the data saved in it will be string then it will be changed to string.

Var in C# Keyword

The var keyword is used to declare a variable but its type is not pre-decided. The Var keyword is a static type keyword. It identifies the variable type at the compile-time and changes its type accordingly. Also, the var type of variable can not be left without initialization. Initialization in terms of programming means, assigning the value to a variable. Kindly take a look at the example below.

Example Program

using System;

namespace Structure_Data_Type
{
    class Program
    {
        static void Main(string[] args)
        {
            var name;
            Console.WriteLine();
        }
    }
}

If you will recreate the above program in your visual studio then it will give you an error that “Implicitly-typed must be initialized“. Look at the screenshot below.

Once you initialize the value of a var variable, it will identify the type of data you are holding in this variable and it will change its type. Let me show you an example.

You can see in the above screen, as soon as we assigned a string to the name variable it has recognized the type of data is a string. It has changed the type as a string too.

Another point to remember about var is that you can not change the variable type a second time. Once the variable is initialized the type of variable is decided. You will have to assign the same type of value inside it.

For example, if you have saved a string in a var type of variable then its type will be string once, and for all, you can not save int in the same variable. The value can be initialized multiple times but the value should be the same type as a string. Let me show you an example below.

Program Example

using System;

namespace Structure_Data_Type
{
    class Program
    {
        static void Main(string[] args)
        {
            var name = "Will Smith";

            name = 123;

            name = "Edward Snoden";

            Console.WriteLine(name);
            Console.ReadLine();
        }
    }
}

If you will recreate the above program in your Visual Studio, it will not let you go to the Edward Snowden line. I will stop you where the value of the name is being assigned an integer 123. It will tell you that it can not convert a string to int. Take a look at the screenshot below.

Dynamic in C# Keyword

I told you two things about the var type variable, the dynamic in C# is just the opposite. First of all, you don’t need to initialize the value of a dynamic variable. Once you have declared any dynamic type variable you can leave it without initialization. It decides the type of the variable at the runtime. Whenever you will run the program it will recognize what type of data is being held in the variable, and it accordingly will change its type.

using System;

namespace Structure_Data_Type
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic name = "Will Smith";

            name = 123;

            name = "Edward Snoden";

            Console.ReadLine();
        }
    }
}

Output

See the output of the above program. The output is “Edward Snowden”. Initially, the type of name variable was a string, then it changed to int, and finally, it changed to string again. It has not given any error what so ever. It proves that the dynamic in C# variable changes its type when we save a different type of data in it.

You must be thinking about where can we use dynamic. Let me give you one example in the form of a program. Take a look at the program below.

using System;

namespace Structure_Data_Type
{

    class Program
    {
        static void Add(dynamic a, dynamic b)
        {
            Console.WriteLine(a+b);
        }
        static void Main(string[] args)
        {
            Add(10.4, 5);
            
            Console.ReadLine();
        }
    }
}

Output

dynamic-variable-program-example-output

Look at the program above, we have created one method with the name Add. We are taking two parameters and the variable types are dynamic. This dynamic in C# variable is giving us the freedom of adding double, float, long, and int types of numbers. We don’t need to create four different methods for all four types of data types in numbers. In the above program, we have added a float and an int that is giving us an output of 15.4.

Final Conclusion about Var vs Dynamic in C#

The Var keyword just gives you the convenience of typing one word for all types of variables. You don’t need to type int, string, float, double, long etcetera. Just type Var and its type will be converted according to the type of data.

The Dynamic in C# keyword gives us the freedom of taking any type of input from the user. Whether it is an integer or decimal data type that you need to save, just declare a dynamic variable and you be worry-free.

I hope the concept of var vs dynamic in C# is clear for you. In case of any confusion, feel free to comment. I will be glad to assist you.

Have a Good one guys. Stay Safe Stay Healthy!