简体   繁体   English

函数重载与可选参数

[英]Function overloading vs Optional Parameters

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? 所以在下面的例子中,为什么重载setName而不是使用中间名和姓氏值的可选参数?

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 仅供参考,这是我重载的第一个函数: 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 这个函数允许你调用带有或不带参数列表的MySqlContext.GetReader() ...我认为它使代码比必须一直调用GetReader(sql, args.ToArray())更整洁

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): 您可以为引用类型设置默认参数,而不是null( string参数除外):

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
    }
}

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. setName(string last)声明给出的最小数据是姓氏,其中public void setName(string first, string middle = "", string last = "")不允许您省略名字。 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") . 如果你想在带有可选参数的方法调用中省略中间名,你必须有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. 但这会破坏名称的自然顺序,并允许您设置中间名而不指定第一个( setName("barack", middle: "hussein"); )三个重载禁止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM