Is passing null in to a method acceptable

前端 未结 5 1977
夕颜
夕颜 2021-01-02 16:03

Null is an odd data type for me, it seems as though it is wrong to ever use, maybe its the null pointer errors i got so often as a beginner that now have me associating any

相关标签:
5条回答
  • 2021-01-02 16:26

    That is perfectly acceptable. If you are using C# 4.0 you can also make the parameter optional so that the caller doesn't need to specify the default value.

    0 讨论(0)
  • 2021-01-02 16:27

    It is absolutely fine to pass null to methods. However, if you have a csae where you might not need to pass a variable, consider overloading.

    The reason why you might pass a null is because you might have a method with quite a few parameters and you do not wan to implement all possible overloads.

    I personally would not use optional parameters - there are many discussions on this very subject because of the issue with the method as Contract which is outside the scope of this question.


    UPDATE

    Correct way to do it is not to duplicate code but let overloads call each other:

    public void Foo(A a)
    {
        Foo(a, null);
    }
    public void Foo(A a, B b)
    {
            // .......
    }
    

    This will tell client of this code:

    • You can call method with A only
    • You can call method with A and B

    If I have only the second method, the client would not know if it is OK to pass null.

    0 讨论(0)
  • 2021-01-02 16:30

    I would argue that this is not ok. Passing null around is never a good practice, and in my opinion whenever a value has null it should be an exception. You might not always have control over how your data is passed, and in those cases you should check for null just in case it could occur. However if you have the option to control the arguments yourself, you should rather overload the method, instead of passing in null.

    Consider the following scenario:

    public string DoSomething(string a, string b)
    {
       // Returns something after a and b is processed.
    }
    

    In this example we're indicating that a and b are strings. They are objects of type char[] or however you would like to define a string. Passing in a null in this method makes no sense at all, since we're expecting strings. A null is nothing - it's not an empty string - it's simply void.

    The reason why I say that I don't think it's preferable to send in null into a method is because there are never any uses for null. It's simply a void left there as a placeholder for something that failed to instansiate. Therefore, calling the method above like this:

    DoSomething("whatever", null) makes no sense at all.

    We could argue that we could do something like this:

    /// <summary>
    /// Does something with string a or b
    /// </summary>
    /// <param name="a">The first string. If it's null, nothing is done with it</param>
    /// <param name="b">The second string. If it's null, nothing is done with it</param>
    /// <returns></returns>
    public string DoSomething(string a, string b)
    {
        // Returns something after a and b is processed.
    }
    

    But that also makes the code less readable. Why not make an overload DoSomething(string a) ? Or maybe refactor the method completely since it can do something with a and null and still return a valid value. null is not a valid value, so hence the result of an operation including null as an argument should not return a valid value.

    It's like using infinity in maths. You will always get infinity as a result if you add/subtract/multiply or divide something with it.

    Disclaimer

    These are my own opinions, and by no means the 'right' way of programming. But since the question has no right answer, I'll practice my right to free speech :)

    0 讨论(0)
  • 2021-01-02 16:40

    Are you saying "is it ok to pass a null in as a parameter to a method?"

    If so - yes - it is perfectly acceptable.

    However, in the .Net 4.0 world, you may want to consider using optional parameters so that you don't even need to pass the null in, you can just omit it.

    In the pre .Net 4.0 world, if it is a common scenario to pass this null into a method, you may want to create an overload that doesn't take the argument at all.

    0 讨论(0)
  • 2021-01-02 16:47

    If you don't use some parameters you can use Overloading or Optional Parameters in C# 4.0

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