Why that is possible to define a class named var or dynamic?

前端 未结 1 1608
无人及你
无人及你 2021-01-23 21:01

Today I noticed we can define a class named var or dynamic. First, I thought it\'s not possible because these are special keywords and cannot be used a

1条回答
  •  时光说笑
    2021-01-23 21:26

    These are called contextual keywords.
    They only behave as keywords if you don't have your own types with those names.

    The point of this is to allow exactly what you just did.

    Because these keywords were introduced by newer versions of C# (3.0 and 4.0), it's possible (though unlikely) that some existing codebases have classes with those names, just like you made.

    Therefore, if the compiler sees such classes, it will compile exactly the way it used to and treat them as regular classes rather than keywords.

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