Why does ReSharper not suggest using null propagation for both of these blocks?

前端 未结 2 2001
一生所求
一生所求 2021-01-13 11:14

ReSharper suggests using null propagation for the "damageable" if block, but for the "forceVelocityCalculator" one there is no suggestion.



        
2条回答
  •  太阳男子
    2021-01-13 12:12

    Probably because of how Unity has defined what comes back from GetComponent()

    If you really really look at what GetComponent does, it actually never returns null, you always get an object wrapper that handles the communication between your C# code and Unity's underlying C++ code.

    So when you get "null" from GetComponent() what you actually get (Unity has overridden the == operator!) is not actually null, but an object wrapper around a null. So ReSharper doesn't know that null propagation would be helpful here (assuming, of course, that it works: because the object isn't actually null, just pretending to be null, the syntactic sugar might not work properly!).

提交回复
热议问题