开发者

Avoiding overload hell with c# 4.0

I am us开发者_如何学编程ing C# 4.0, how can I avoid the problem of writing lots of similiar methods because they are each parameter-unique (how can the new parameter features avoid overload hell?).

Thanks


Instead of this:

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

You can just have one method with all the params you want, and call it using the named params like this:

void Method(string param1, string param2 = "default2", 
            string param3 = "default3", int int4 = 12, int lastParam = 12) { }

And call it like this:

Method(param1: "myString", int4: 23);
//or...
Method(param1: "myString", param4: "string2", int4: 23);

Just include what you want to set, the rest will be the defaults you specified in the method signature.


Assume you have a class Employee as mentioned below which has 3 constructors.

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

With C# 4.0, you will need to create a single constructor as following which will replace all 3 constructors.

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

This constructor can be called in following manners..

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");


In C# 4.0, you can use optional parameters.

Named and Optional Arguments (C# Programming Guide)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜