Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding overload hell with c# 4.0

Tags:

c#

c#-4.0

I am using 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

like image 529
GurdeepS Avatar asked May 02 '10 15:05

GurdeepS


3 Answers

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.

like image 54
Nick Craver Avatar answered Nov 18 '22 16:11

Nick Craver


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");
like image 27
this. __curious_geek Avatar answered Nov 18 '22 15:11

this. __curious_geek


In C# 4.0, you can use optional parameters.

Named and Optional Arguments (C# Programming Guide)

like image 3
Thomas Avatar answered Nov 18 '22 15:11

Thomas