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

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

    What if the DependencyObject depObj was actually a FrameworkOtherTypeOfElement

    Then depObj would not be null

    but the attempted as Casts would both evaluate to null and _fe & _fce would both be null

    as is equivalent to doing

    if(I Can Cast This Object)
       //Then cast it
    else
       //Return null
    

提交回复
热议问题