EDUDOTNET

Thursday, October 17, 2013

What different between Var, Object and Dynamic data type in c#? How to use??

The data types (var, object and dynamic) are generally used while you are working
with anonymous type.

Var:
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:
namespace EDUDOTNET_Var_Object_Dynamic
{
    class Program
    {
        static void Main(string[] args)
        { 
            /// <summary>
            /// Example of var type
            /// </summary>            
            // assign a number value
            var varItem = 100;
            //does not require explicit type casting for any operation at runtime
            varItem = varItem + 100;
            //varItem ="EDUDOTNET";//We cannot change the type of these variables at runtime. 
 
            /// <summary>
            /// Example of object type
            /// </summary>
            // assign a number value
            object objItem = 100;
            //require explicit type casting for any operation at runtime
            objItem = (int)objItem + 100;
            //object type does not allows you to perform any operations 
            //objItem += 100; 
            //[Error: Operator '+=' cannot be applied to operands of type 'object' ana 'int']
            objItem = "EDUDOTNET";
 
            /// <summary>
            /// Example of dynamic type
            /// </summary>
            // assign a number value
            dynamic dynItem = 100;
            //does not require explicit type casting for any operation at runtime
            dynItem = dynItem + 100;
            // dynamic type allows you to perform any operations
            dynItem += 100;
            dynItem = "EDUDOTNET";
        }
    }
}



0 comments:

Post a Comment