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

后端 未结 6 1996
囚心锁ツ
囚心锁ツ 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:49

    First, the as keyword includes a is check.

    if( o is A)
       a = (A) o;
    

    is the same as

    a = o as A;
    

    Second, as does not convert the Type like a cast does, even if a conversion operator from Type A to B is defined.

提交回复
热议问题