Introduction
The Object type has existed since the beginning of C#, in other words, C# 1.0, and exists at System. Object. Var comes next in C# 3.0, followed by the dynamic keyword in C# 4.0.
Object
Now object comes from C# 1.0 and derives from System. Object, and we can assign any value to this reference.
using System;
class Program
{
static void Main(string[] args)
{
object iobj = 10; // Perfectly Legal
var ivar = 10;
dynamic dvar = 10;
Console.WriteLine(iobj.GetType()); // Gives System.Int32.
Console.WriteLine(ivar.GetType()); // Gives System.Int32.
Console.WriteLine(dvar.GetType()); // Gives System.Int32.
// 1)
// iobj = iobj + 10; // Illegal and gives a compile-time error.
// Reason: At compile time, it is of type object, so the compiler doesn't know how to add object type.
// You need to cast it to an integer to perform the addition.
iobj = (int)iobj + 10;
Console.WriteLine(iobj); // Perfectly legal
// 2)
ivar = ivar + 10; // Perfectly legal
// Reason: At compile time, it is not of type var but int.
Console.WriteLine(ivar);
// 3)
dvar = dvar + 10; // Perfectly legal
// Reason: At compile time, it is not of type var but int.
Console.WriteLine(dvar);
Console.ReadLine();
}
}
What's the difference between var and dynamic?
- var was introduced in C# 3.0. Dynamic was introduced in C# 4.0.
- var is statically typed; in other words, the data type of var is inferred at compile time. Dynamic is a dynamic type, and the value type is inferred at runtime.
- Vars are initialized during declaration. A dynamic can be initialized at runtime.
- Var cannot be used to create a return type of a function or property. Dynamic can be used as the return type or properties.
- var cannot change the data type. A dynamic can change its data type later.
using System;
class Program
{
static void Main(string[] args)
{
// Using var
// var val = 10; // This would infer the type as int
// val = "string"; // This line would be illegal, as you cannot assign a string to an int variable.
// Using dynamic
dynamic dynam = 10; // Dynamically typed variable
dynam = "string1"; // Perfectly legal, as the type is determined at runtime
Console.WriteLine(dynam); // Will print "string1"
Console.ReadLine();
}
}
As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time." -MSDN.
Alex LiPosted Jul 13, 2017, 3:40 AM
Good Article Thank you
Yashwanth MuthineniPosted Aug 19, 2015, 6:10 AM
Nice Share
Shakti SaxenaPosted Aug 18, 2015, 7:47 AM
Good and Informative article
Gowtham KPosted Aug 17, 2015, 4:30 AM
Nice Share Thank You
Nilesh JadavPosted Aug 17, 2015, 4:24 AM
Good Article sir
Sibeesh VenuPosted Aug 17, 2015, 4:13 AM
Nice Share. Thank you.
Vishal VermaPosted Aug 17, 2015, 3:24 AM
Thanks all :) .really Motivating
Ankit BansalPosted Aug 17, 2015, 1:26 AM
thanks for share...
Vaikesh K PPosted Aug 17, 2015, 12:37 AM
Good one
Mohammed IbrahimPosted Aug 16, 2015, 11:14 PM
nice
Shakti SaxenaPosted Aug 16, 2015, 1:20 PM
nice share :)
Pankaj Kumar ChoudharyPosted Aug 16, 2015, 5:11 AM
Nice one vishal
Karthikeyan KPosted Aug 16, 2015, 3:04 AM
Good one..thanks for sharing
Santhakumar MunuswamyPosted Aug 16, 2015, 2:19 AM
Nice Article. Thanks for sharing
Gowtham KPosted Aug 16, 2015, 1:46 AM
Good one
Rajeesh MenothPosted Aug 16, 2015, 1:37 AM
Nice One....
Gopi ChandPosted Aug 15, 2015, 11:43 PM
Nice one