Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用c#4.0避免过载地狱_C#_C# 4.0 - Fatal编程技术网

用c#4.0避免过载地狱

用c#4.0避免过载地狱,c#,c#-4.0,C#,C# 4.0,我使用的是C#4.0,我如何避免编写大量类似方法的问题,因为它们每个参数都是唯一的(新的参数特性如何避免过载?) 谢谢你,而不是这个: void Method(string param1, string param2) { } void Method(string param1, string param2, string param3) { } void Method(string param1, string param2, string param3, string param4) { }

我使用的是C#4.0,我如何避免编写大量类似方法的问题,因为它们每个参数都是唯一的(新的参数特性如何避免过载?)

谢谢你,而不是这个:

void Method(string param1, string param2) { }
void Method(string param1, string param2, string param3) { }
void Method(string param1, string param2, string param3, string param4) { }
void Method(string param1, string param2, string param3, int int4) { }
//etc...
您可以只使用一个方法和所有想要的参数,并使用命名参数调用它,如下所示:

void Method(string param1, string param2 = "default2", 
            string param3 = "default3", int int4 = 12, int lastParam = 12) { }
Method(param1: "myString", int4: 23);
//or...
Method(param1: "myString", param4: "string2", int4: 23);
这样称呼它:

void Method(string param1, string param2 = "default2", 
            string param3 = "default3", int int4 = 12, int lastParam = 12) { }
Method(param1: "myString", int4: 23);
//or...
Method(param1: "myString", param4: "string2", int4: 23);

只需包含您想要设置的内容,其余内容将是您在方法签名中指定的默认值。

假设您有一个类Employee,如下所述,它有3个构造函数

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Qualification { get; set; }
    public string MiddleName { get; set; }

    public Employee(string firstName, string lastName)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= "N/A";
        MiddleName= string.Empty;
    }
    public Employee(string firstName, string lastName, string qualification)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName= string.Empty;

    }
    public Employee(string firstName, string lastName, string qualification,
        string middleName)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName= middleName
    }
}
public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Qualification { get; set; }
    public string MiddleName { get; set; }

    public Employee(string firstName, string lastName,
            string qualification = "N/A", string middleName = "")
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName = middleName;
    }
}
使用C#4.0,您需要创建一个构造函数,如下所示,它将替换所有3个构造函数

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Qualification { get; set; }
    public string MiddleName { get; set; }

    public Employee(string firstName, string lastName)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= "N/A";
        MiddleName= string.Empty;
    }
    public Employee(string firstName, string lastName, string qualification)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName= string.Empty;

    }
    public Employee(string firstName, string lastName, string qualification,
        string middleName)
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName= middleName
    }
}
public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Qualification { get; set; }
    public string MiddleName { get; set; }

    public Employee(string firstName, string lastName,
            string qualification = "N/A", string middleName = "")
    {
        FirstName= firstName;
        LastName= lastName;
        Qualification= qualification;
        MiddleName = middleName;
    }
}
可以通过以下方式调用此构造函数

Employee emp = new Employee("Adil", "Mughal");
Employee emp = new Employee("Adil", "Mughal", middleName: "Ahmed");
Employee emp = new Employee("Adil", qualification:"BS");
Employee emp = new Employee("ABC", lastName: "EFG", qualification: "BS");
Employee emp = new Employee("XYZ", middleName: "MNO");
在C#4.0中,可以使用可选参数


我认为语法中没有大括号。@Martinho-Woops同时回答了一个javascript对象问题:)修复了如果该方法使用了所有参数,而我尝试只传入其中一些参数会怎么样?@dotnetdev-将默认值设为所有可能无法传入的参数,当您不指定特定参数时,这些默认值将被使用/传递到方法中,即使默认值是
null
,因为你已经可以在C#3中对属性进行初始化了。对于你的第一个版本,我会让参数较少的构造函数调用参数较多的构造函数并传递默认值。这是一个无用的观点,因为第二个选项更好,但只是在星期天吹毛求疵:)在C#3中,我可能会考虑重写该方法以接受参数集合对象。像Nick Comments一样,可以使用属性初始值设定项方便地初始化该对象。但是对于C#4.0来说,直接在方法中使用可选参数似乎更明智。