Is there more to the C# “as” keyword than simple casting?

后端 未结 6 1987
囚心锁ツ
囚心锁ツ 2021-02-05 22:26

I\'m working through Josh Smith\'s CommandSink code obviously do not understand something about the \"as\" keyword in C#.

I don\'t understand why he wrote the line:

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 22:41

    as does "cast, if it is", and equivalent to:

    (X is TYPE) ? (TYPE) X : null

    it is however, more efficient than is + cast.

    depObj may implement either interface, none, or both.

提交回复
热议问题