Interviews15 answers
Difference between var and dynamic?
15 Answers
Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed.
Suresh KumarPosted Oct 29, 2017, 10:06 AM EDT
Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed.
Suresh KumarPosted Oct 29, 2017, 10:05 AM EDT
dynamic variables are dynamically typed and need not to be initialized, but var is implicitly typed and must be initialized
Praveen KumarPosted Sep 10, 2017, 9:58 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
var is compile time bound variable. One big difference - you can have a dynamic return type. var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type. dynamic is a new (static) type, where all checks are done at runtime, not by the compiler
Arvind YadavPosted Jun 30, 2017, 8:44 PM EDT
Var - once you define the value to this variable then you cannot change those again, if you change it gives error on compile time.
Eg - var d= 1; d = "i am changed"; //error on compile time
Dynamic - Once you define the value to this variable then you can easily change the value on run time.
Eg - Dynamic d= 1; d = "i am changed"; //it works fine no error
Pankaj SapkalPosted Jun 25, 2017, 10:19 PM EDT
The type of Var variable is resolved at compile time.
See the below example:
var i = 10; // the type of i is resolved to int.
The below piece of line will give compile time error.
i = "abc"; //Since the type of i is already resolved to int, this code is invalid.
In case of dynamic, the type of the variable is resolved at runtime.
See the below example:
dynamic i = 10; //The type of i is resolved as int at runtime.
The below line of code doesn't raise any compile time errors.
i = "abc"; // Since the type is resolved dynamically, this is absolutely fine.
Mohammad IlyasPosted Jun 14, 2017, 9:20 PM EDT
var dynamic
Introduced in C# 3.0
Introduced in C# 4.0
Statically typed – This means the type of variable declared is decided by the compiler at compile time.
Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.
Need to initialize at the time of declaration.
e.g., var str=”I am a string”;
Looking at the value assigned to the variable str, the compiler will treat the variable str as string.
No need to initialize at the time of declaration.
e.g., dynamic str;
str=”I am a string”; //Works fine and compiles
str=2; //Works fine and compiles
Errors are caught at compile time.
Since the compiler knows about the type and the methods and properties of the type at the compile time itself
Errors are caught at runtime
Since the compiler comes to about the type and the methods and properties of the type at the run time.
Visual Studio shows intellisense since the type of variable assigned is known to compiler.
Intellisense is not available since the type and its related methods and properties can be known at run time only
e.g., var obj1;
will throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.
e.g., dynamic obj1;
will compile;
e.g. var obj1=1;
will compile
var obj1=” I am a string”;
will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.
e.g. dynamic obj1=1;
will compile and run
dynamic obj1=” I am a string”;
will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.
This code will work fine.
Rajkiran SwainPosted Jun 7, 2017, 3:10 AM EDT
Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed.
Shamim UddinPosted Apr 12, 2017, 6:17 AM EDT
var : assign the value at the time of declaration because compiler is checking at compile time. once we assign the value in var object it cannot re-assign the value of ther type.
example :
var test="name"; // assign value as string type
var a; /// error at compile time
var b="test";
b=123 // error
Dynamic: it dynamic type. it is not necessary to assign the value at declaration time. It is bypassing all the type checking at compile time and resolves everything at run time.
example:
dynamic a="test";
a=123 // no error
dynamic a;
a="ssdd";
a=123; // no error
sushil kumarPosted Apr 9, 2017, 4:49 AM EDT
dynamic test = 1;
test = "i'm a string now"; // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
A var is an implicitly typed variable that understands by the compiler. It is just as strongly typed as if you explicitly typed it yourself using “int test2 = 2;”. Whereas a dynamic variable bypasses all compile time type checking and resolves everything at runtime.
Santosh Kumar AdidawarpuPosted Feb 11, 2017, 2:54 AM EST







