C# Dynamic Keyword — Run-time penalty?

后端 未结 6 1188
春和景丽
春和景丽 2020-12-01 14:20

Does defining an instance as dynamic in C# mean:

  1. The compiler does not perform compile-time type checking, but run-time checking takes place like it always

6条回答
  •  离开以前
    2020-12-01 15:01

    Declaring a variable as dynamic is similar to declaring it as object. Dynamic simply gets another flag indicating that member resolution gets deferred to run-time.

    In terms of the performance penalty - it depends on what the underlying object is. That's the whole point of dynamic objects right? The underlying object can be a Ruby or Python object or it can be a C# object. The DLR will figure out at run-time how to resolve member calls on this object and this resolution method will determine the performance penalty.

    Having said that - there definitely is a performance penalty.

    That's why we're not simply going to start using dynamic objects all over the place.

提交回复
热议问题