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 |
4Let's Try It in C#
Here we store the same product price using all three approaches.
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);
}
}
5Check the Program Result
The first line shows the inferred type. The next two lines add 500 to the stored price.
Int32 2000 2000 Hello
6Watch the Code Execute
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
Can var store different types later?
Answer No. Its type is fixed during compilation based on the initial value.
Is var the same as dynamic?
Answer No. var is statically typed. dynamic uses runtime binding.
What is boxing in C#?
Answer Boxing converts a value type, such as int, into an object reference. Unboxing retrieves the value type.
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.
Can we declare var without assigning a value?
Answer No. A local var declaration needs an initializer so the compiler can infer its type.