函数重载与可选参数 [英] Function overloading vs Optional Parameters

查看:78
本文介绍了函数重载与可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是在考虑函数重载...

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.

那么在下面的示例中,为什么要重载setName而不是对中间名和姓氏值使用可选参数?

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).

有人可以解释吗?

仅供参考,这是我有史以来第一个重载的函数: http://pastebin.com/ynCuaay1
此功能允许您调用带有或不带有参数列表的MySqlContext.GetReader() ...我认为这使代码比必须一直调用GetReader(sql, args.ToArray())整整

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

推荐答案

我不明白为什么它会比使用可选参数更可取

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.

您可以为引用类型设置默认参数,而不是null(string参数除外):

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

这篇关于函数重载与可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆