Cast vs 'as' operator revisited

后端 未结 5 979
长情又很酷
长情又很酷 2020-12-24 02:33

I know there are several posts already concerning the difference between casts and the as operator. They all mostly restate the same facts:

  • The
5条回答
  •  不思量自难忘°
    2020-12-24 03:29

    Should we suspect that the cast is resolved, wholly or partly, at compile time and the as operator not?

    You gave the answer yourself at the beginning of your question: "The as operator will not use user-defined conversion operators" - meanwhile, the cast does, which means it needs to find those operators (or their absence) at compile time.

    Note that as far as the compiler is concerned, there is no known connection between the (unknown at compile-time) type T and Bar.

    The fact that the T type is unknown means the compiler can't know whether or not there is no connection between it and Bar.

    Note that (Bar)(object)foo does work, because no type can have a conversion operator to Object [since it is the base class of everything], and the cast from object to Bar is known to not have to deal with a conversion operator.

提交回复
热议问题