Function overloading vs Optional Parameters

倾然丶 夕夏残阳落幕 提交于 2020-01-01 12:11:13

问题


So I'm just thinking about function overloading...

Overloaded methods share the same name but have a unique signature. The number of parameters, types of parameters or both must be different. A function can't be overloaded on the basis of a different return type alone.

So in the following example, why overload setName rather than use optional parameters for the middle and last name values?

class funOverload
{
    public string name;

    //overloaded functions
    public void setName(string last)
    {
        name = last;
    }

    public void setName(string first, string last)
    {
        name = first + "" + last;
    }

    public void setName(string first, string middle, string last)
    {
        name = first + "" + middle + "" + last;
    }

    //Entry point
    static void Main(string[] args)
    {
        funOverload obj = new funOverload();

        obj.setName("barack");
        obj.setName("barack "," obama ");
        obj.setName("barack ","hussian","obama");

    }
}

At the very least, using the following would cut down on the amount of code that needs to be written:

public void setName(string first, string middle = "", string last = "")
{
   name = first + "" + middle + "" + last;

   // name = "barack" + "" + "";
}

//Entry point
static void Main(string[] args)
{
    funOverload obj = new funOverload();

    // could optionally set middle and last name values here as well
    obj.setName("barack");
 }

I understand the concept of overloading, but I what I don't get is why it would be a more desirable option than using optional parameters (or vice versa).

Can anyone explain?

Just for reference, here's the first function I ever overloaded: http://pastebin.com/ynCuaay1
This function allows you to call MySqlContext.GetReader() with or without a list of parameters... I thought it made the code a lot neater than having to call GetReader(sql, args.ToArray()) all the time


回答1:


I don't get is why it would be a more desirable option than using optional parameters

Parameters with default values have some limitations, which can be significant in some cases.

You can set default parameter for reference type, other than null (except string parameters):

class Foo
{
    public int Id { get; set; }
}

class Bar
{
    public Bar(Foo parent)
    {
    }

    public Bar()
        : this(new Foo { Id = 1 }) // this can't be done with default parameters
    {
    }
}

Parameters with default values can't appear before regular parameters, while this can be suitable sometimes:

class Foo
{
    public void Method(int i, string s, bool b) { }
    public void Method(string s, bool b) 
    {
        Method(0, s, b); // this can't be done with default parameters
    }
}



回答2:


In your example the three overloads aren't equivalent to the method with optional parameters. setName(string last) states that the minimum data given is the last name, where public void setName(string first, string middle = "", string last = "") doesn't allow you to ommit first name. If you want to ommit middle name in the call of the method with optional parameters you would have to have setName("barack", last: "obama").

It would be somewhat better to have method with optional parameters like this:

public void setName(string last, string first= "", string middle = "")

But this ruins the natural order of names and allows you to set middle name without specifing first (setName("barack", middle: "hussein");) which the three overloads prohibit.



来源:https://stackoverflow.com/questions/16789341/function-overloading-vs-optional-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!