How do I create a Null Object in C#

后端 未结 5 1476
南笙
南笙 2020-12-16 00:07

Martin Fowler\'s Refactoring discusses creating Null Objects to avoid lots of

if (myObject == null)

tests. What is the right way to do th

5条回答
  •  时光取名叫无心
    2020-12-16 00:46

    Go look up the amount of pain that interesting concepts, such as DbNull, have caused and think about if this is actually a good idea.

    Protip: if you are constantly checking for null references, you probably should rethink the API a bit to help preclude null objects closer to the top of the stack.

    Protip II: having something throw an exception when there is an unexpected null is actually fine and dandy. Things should go boom if you have nulls where there shouldn't be null.

提交回复
热议问题