LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Var vs Dynamic vs Object in C#

All three can store values, but they work differently. Understanding these differences helps us write safer code, avoid runtime errors and choose the right variable type in our applications.

1Understanding var, dynamic and object

Hi friends, in this article we will learn the difference between var, dynamic and object in C#. All three can store values, but they work differently. Understanding these differences helps us write safer code, avoid runtime errors and choose the right variable type in our applications.

2Think About a Shopping App

Imagine a shopping app storing a product price, product details and a value received from an external service.

For a known price, we can use var and let C# identify its type. For a value that may contain different data types, we can use object. When the type is determined at runtime, dynamic can be useful.

3How These Three Types Differ

1. var – Compile-time type inference

C# identifies the variable type from its assigned value during compilation. After that, its type cannot change.

2. object – Base type of C# types

It can hold values of different types. Value types such as int are boxed when stored and must be unboxed or cast to use as their original type.

3. dynamic – Runtime binding

The compiler skips normal member checking for dynamic operations. C# checks them at runtime, so an invalid operation can cause a runtime error.

Feature var object dynamic
Type decided Compile time Runtime value Runtime
Needs casting No Usually No
Member checking Compile time After casting Runtime
Type can change No Yes Yes
Remember var is not a dynamic type. Its actual type is fixed when the compiler infers it.

4Let's Try It in C#

Here we store the same product price using all three approaches.

Program.csC#
using System;

class Program
{
    static void Main()
    {
        // Type inferred as int
        var price = 1500;

        // Stored as object
        object productPrice = 1500;

        // Type checked at runtime
        dynamic dynamicPrice = 1500;

        Console.WriteLine(price.GetType().Name);
        Console.WriteLine(((int)productPrice) + 500);
        Console.WriteLine(dynamicPrice + 500);

        dynamicPrice = "Hello";
        Console.WriteLine(dynamicPrice);
    }
}
Pro Tip Use var when the type is clear from the right-hand side. It improves readability without sacrificing type safety.

5Check the Program Result

The first line shows the inferred type. The next two lines add 500 to the stored price.

Output
Int32
2000
2000
Hello
Common Mistake Assigning a string to dynamic is allowed, but trying to perform an invalid operation on it can fail at runtime.

6Watch the Code Execute

Program.cs - Visual Studio Code
● Program.cs
▤⌕⑂⚙
 
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
 
Ln 1, Col 1  UTF-8  C#

7Quick Revision

Use var when the compiler can infer a clear type. Use object when you need a general-purpose storage type. Use dynamic when runtime binding is genuinely required.

  • var gives compile-time type safety.
  • object often needs casting to access members.
  • dynamic moves member checking to runtime.

8Questions Interviewers May Ask

Q1Easy

Can var store different types later?

Answer No. Its type is fixed during compilation based on the initial value.

Q2Easy

Is var the same as dynamic?

Answer No. var is statically typed. dynamic uses runtime binding.

Q3Medium

What is boxing in C#?

Answer Boxing converts a value type, such as int, into an object reference. Unboxing retrieves the value type.

Q4Medium

When can dynamic throw an exception?

Answer When a requested member or operation is invalid at runtime. For example, calling a missing method can throw RuntimeBinderException.

Q5Hard

Can we declare var without assigning a value?

Answer No. A local var declaration needs an initializer so the compiler can infer its type.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.