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;
}
}
|
Object |
VAR |
Dynamic |
Introduce in Framework |
C# 1.0 |
C#3.0 |
C#4.0 |
Stored value type |
It can store any type of value because it is base class i.e. All types in .net framework are derived from object. |
"It can store any type of value, but We must initialize ""var"" types at the time of declaration." |
It can store any type of value just like old VB language variables. |
The compiler has information about the type? |
Compiler has very little information about the type. |
It is type safe. It means that the compiler has all information about the type so that there is no issue at run time. |
It is not type safe. It means that compiler doesn't have any information about the type of variable. |
Casting |
Need to cast object variable to original type when use it. |
No need to cast because the compiler has all information about the type. |
Casting is not required, but we need to know the properties and methods related to stored type because intelligence is not available for dynamic type. |
Intellisense support? |
No |
Yes |
No |
Cause the problem at run time? |
Yes. It throws runtime exception when compiler is not able to convert values from stored value type to required type. |
No, because type is already known by compiler. |
Yes. It throws runtime exception When properties or method name are mismatched from original. |
Usefulness |
It is very useful when we don't have more information about the type. |
It is useful when we don't know the actual type. |
It is very useful when we are using dynamic language for coding or use COM component in our code. |
Does it pass to a method or create typed properties? |
Yes. It can be passed as method parameter and method also can return object type. |
"No, we cannot declare a method parameter or return type as ""VAR""" |
"Yes, we can declare a method parameter or return type as ""VAR""" |