About the non-nullable types debate

前端 未结 7 526
北海茫月
北海茫月 2020-12-20 18:07

I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion

相关标签:
7条回答
  • 2020-12-20 18:39

    I admit that I haven't really read a lot about Spec#, but I had understood that the NonNullable was essentially an attribute that you put on a parameter, not necessarily on a variable declaration; Turn your example into something like:

    class Class { ... }
    
    void DoSomething(Class c)
    {
        if (c == null) return;
        for(int i = 0; i < c.count; ++i) { ... }
    }
    
    void main() {
        Class c = nullptr;
        // ... ... ... code ...
        DoSomething(c);
    }
    

    With Spec#, you are marking doSomething to say "the parameter c cannot be null". That seems like a good feature to have to me, as it means I don't need the first line in the DoSomething() method (which is an easy to forget line, and completely meaningless to the context of DoSomething()).

    0 讨论(0)
提交回复
热议问题