dynamic vs object type

前端 未结 4 1077
清酒与你
清酒与你 2020-12-01 14:10

I have used the dynamic and the object type interchangeably. Is there any difference between these two types? Is there any performance implications of using one over the oth

相关标签:
4条回答
  • 2020-12-01 14:24

    With the advancement in C# language we have see that even var type is comparable to dynamic and object types. Here are all 3 types as I learned by comparing these 7 points:

    Object

    1. Object was introduced with C# 1.0.
    2. It can store any kind of value, because object is the base class of all type in .NET framework.
    3. Compiler has little information about the type.
    4. Object type can be passed as method argument and method also can return object type.
    5. Need to cast object variable to original type to use it and performing desired operations.
    6. Cause the problem at run time if the stored value is not getting converted to underlying data type.
    7. Useful when we don’t have more information about the data type.

    Var

    1. Var was introduced with C# 3.0
    2. It can store any type of value but It is mandatory to initialize var types at the time of declaration.
    3. It is type safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
    4. Var type cannot be passed as method argument and method cannot return object type. Var type work in the scope where it is defined.
    5. No need to cast because compiler has all information to perform operations.
    6. Doesn't cause problem because compiler has all information about stored value.
    7. Useful when we don’t know actual type i.e. type is anonymous.

    Dynamic

    1. Dynamic was introduced with C# 4.0
    2. It can store any type of the variable, similar to old VB language variable.
    3. It is not type safe i.e. Compiler doesn't have any information about the type of variable.
    4. Dynamic type can be passed as method argument and method also can return dynamic type.
    5. Casting is not required but you need to know the properties and methods related to stored type.
    6. Cause problem if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
    7. Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.

    Hopefully this would help somebody.

    Thanks!

    0 讨论(0)
  • 2020-12-01 14:25

    They're hugely different.

    If you use dynamic you're opting into dynamic typing, and thus opting out of compile-time checking for the most part. And yes, it's less performant than using static typing where you can use static typing.

    However, you can't do much with the object type anyway - it has hardly any members. Where do you find yourself using it? When you want to write general purpose code which can work with a variety of types, you should usually consider generics rather than object.

    0 讨论(0)
  • 2020-12-01 14:29

    In simple language:
    Assume we have the following method:

    public static void ConsoleWrite(string inputArg)
    {
        Console.WriteLine(inputArg);
    }
    

    Object: the following code has compile error unless cast object to string:

    public static void Main(string[] args)
    {
        object obj = "String Sample";
        ConsoleWrite(obj);// compile error
        ConsoleWrite((string)obj); // correct
        Console.ReadKey();
    }
    

    dynamic: the following code compiles successfully but if it contains a value except string it throws Runtime error

    public static void Main(string[] args)
    {
        dynamic dyn = "String Sample";
        ConsoleWrite(dyn); // correct
        dyn = 1;
        ConsoleWrite(dyn);// Runtime Error
        Console.ReadKey();
    }
    
    0 讨论(0)
  • 2020-12-01 14:29

    There is an article explaining the different types, including object and dynamic types. The article also explains the difference between the two with a nice example.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types

    I shall give a short gist of the difference explained in the article:

    • The object type is an alias for System.Object in .NET. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from System.Object. You can assign values of any type to variables of type object.

    • The dynamic type indicates that use of the variable and references to its members bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).

    • Type dynamic behaves like type object in most circumstances. In particular, any non-null expression can be converted to the dynamic type. The dynamic type differs from object in that operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

    Summary:

    What this essentially means is object is a type and all other types inherit from it. Dynamic is not really a type, it is more like a pointer or representation of some other type, which will be resolved at run time.

    0 讨论(0)
提交回复
热议问题