Introduction to object
The object class is the base class for all ohter classes; in other words all derived types are inherited from the object base type. We can assign values of any type to the object type variable (when the value type is converted to an object it is called "boxing" and when the variable type object is converted into any other value type is called "unboxing").
Example
object a = 10;
object b = "This is test string";
object c = a;
//Below is also possible with object type
object k = "jignesh";
k = 10;
Introduction to var keyword
The var keyword was introduced in C# 3.0. It is an implicit type and the type is determined by the C# compiler. There is no performance penalty when using var. The C# compiler assigns types to the variable when it assigns the value.
Example
var a = 10; // it is same as int a = 10;
var b = "This is test string";
var c = b; // gives compilation error.
var words = new string[] { "apple", " banana", "peach", " grape" };
It makes programs shorter and easier to read. In short the var keyword is an implicit way of defining data types. It is an indirect way of defining the variable type. The “var” keyword defines the data type statically (at compile time) and not at runtime.
The var keyword is very useful when.
- We have a long class name so that our code is not so readable. var makes it short and sweet.
- We use LINQ and anonymous types that help us reduce the code (reduce effort to create a new class).
Introduction to Dynamic Keyword
C# 4.0 introduced a new type called "Dynamic". Dynamic is a new static type that is not known until runtime. At compile time, an element with dynamic is assumed to support any operation so that we need not be worried about the object, whether it get the value from COM, the DOM, from reflection or anything else in the program. Errors are caught at runtime only. The dynamic type in C# uses the Dynamic Language Runtime (DLR) introduced as a new API in the .Net Framework 4.0. Type dynamic behaves like an object in most cases, but the expression of type dynamic is not checked by the compiler. The type's dynamic status is compiled into a variable type object that is dynamic at compile time, not at run time.
At compilation a dynamic is converted into a System.object and the compiler will emit the code for type safety during runtime. It suffers from boxing and unboxing because it is treated as a System.Object and also the application's performance will suffer siince the compiler emits the code for all the type safety.
Example
dynamic i = 10;
dynamic s = "Jignesh Trivedi";
string p = "This is test";
dynamic k = p;
dynamic other = 10; //assign int value so run time type is integer
other = "Jignesh"; // now runtime type of other varible become string.
We can define a dynamic variable, property, field and parameter, return value, type constraint and so on.
// A dynamic field.
static dynamic field;
// A dynamic property.
dynamic name { get; set; }
// A dynamic return type and a dynamic parameter type.
public dynamic Method(dynamic parameter)
{
dynamic local = "return string";
int other = 0;
if (parameter is string)
{
return local;
}
else
{
return other;
}
} 
Chandan HasijaPosted Oct 18, 2019, 4:02 AM
I tried to compile the example of var, there was no compilation error for var c = b;.
Vithal WadjePosted Jan 7, 2015, 10:34 PM
nice one ,another thing is that var detects the value at compile time where as dynamic type detects the datatype at run time
Sam HobbsPosted Jan 7, 2015, 7:34 PM
var appears to me to be something that makes programs frustratingly confusing. I avoid var whenever possible. I would not enjoy working with a program that uses var very much. Can you explain how to determine what something is by looking at the declaration? Or must we hunt through the execution to determine what a var will be during runtime? Does VS have a way to tell us what a var will be?Also, can you explain why var is not used for everything? Also, am I correct that a var cannot be returned as a var or has that changed?
Rasmita DashPosted Jan 7, 2015, 5:13 AM
gud comparison...just a typo in the last comparison. We can declare a method parameter or return type as "dynamic". In the image it's "var" instead of "dynamic". It created confusion for a moment...
Michal HabalcikPosted Jan 7, 2015, 4:15 AM
great comparison. One thing to add - var keyword makes the code more readable, but only until not overused. Otherwise the code becomes extremelly unreadable.