Variable, Object and Dynamic data type in C#

Variable, Object and Dynamic data type in c#
 
The data types (var, object and dynamic) are generally used while you are working
with anonymous type.

Variable: This is a data type, var data type introduced since .Net Framework 3.0. Var datatype is pre-determined at compilation time. We cannot leave it unassigned or having a null value we need to implicitly assign a value to it otherwise we`ll receive an error. Var does not require explicit type casting for any operation at run-time and we cannot change the type of these variables at run-time.

Object: This is a root data type, object data type introduced since .Net Framework 2.0. Object data type is determined at run time. This is a alias for System.Object and object is the root of all classes (root data type).
In this we need to do boxing and unboxing to retrieve the actual value. The Object does not allow you to perform any operation such as mathematical operation without doing"boxing and unboxing". Its means we require explicit type casting for any operation at run-time. Useful when doesn't have more information about the data type.

Dynamic: This is a data type, dynamic data type introduced since .Net Framework 4.0. The dynamic data type allows you to perform any operations and will be resolved at run time. It does not require explicit type casting for any operation at run-time, because it identifies the types at run-time only. Dynamic type can be passed as
function argument and function also can return object type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code. 
 
Example Code
  1. namespace EDUDOTNET_Var_Object_Dynamic  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {   
  7.             /// <summary>  
  8.             /// Example of var type  
  9.             /// </summary>              
  10.             // assign a number value  
  11.             var varItem = 100;  
  12.             //does not require explicit type casting for any operation at runtime  
  13.             varItem = varItem + 100;  
  14.             //varItem ="EDUDOTNET";//We cannot change the type of these variables at runtime.   
  15.    
  16.             /// <summary>  
  17.             /// Example of object type  
  18.             /// </summary>  
  19.             // assign a number value  
  20.             object objItem = 100;  
  21.             //require explicit type casting for any operation at runtime  
  22.             objItem = (int)objItem + 100;  
  23.             //object type does not allows you to perform any operations   
  24.             //objItem += 100;   
  25.             //[Error: Operator '+=' cannot be applied to operands of type 'object' ana 'int']  
  26.             objItem = "EDUDOTNET";  
  27.    
  28.             /// <summary>  
  29.             /// Example of dynamic type  
  30.             /// </summary>  
  31.             // assign a number value  
  32.             dynamic dynItem = 100;  
  33.             //does not require explicit type casting for any operation at runtime  
  34.             dynItem = dynItem + 100;  
  35.             // dynamic type allows you to perform any operations  
  36.             dynItem += 100;  
  37.             dynItem = "EDUDOTNET";  
  38.         }  
  39.     }  
  40. }  
Ebook Download
View all
Learn
View all