4
votes

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?

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

2
It depends, optional parameters are good for things that were previously overloads and just called it with the extra parameter's default value. Here they would fit, but there are some places where they wouldn't (e.g. something that uses an XmlWriter internally but can take a TextWriter), such as XDocument.Save - It'sNotALie.
Who says one is more desirable than the other? I think it's just a matter of context and preference really. - John Willemse
Apart from the other comments, optional parameters were not available till c# 4.0, and those of us who learned on earlier versions got used to writing overloaded functions. - Jcl
Optional parameters lend themselves more toward cases where sensible or common default values exists. Whereas overloading indicates you actually have several forms of the same function depending upon the existence of certain parameter information. In most cases either would work, but one may be noticeably clearer than the other in a given context. - lurker
There are also fun tidbits; optional parameters are resolved at compile time. So if you compile against a DLL that had string middle = "", then swap it for an updated version that has string middle = null, without recompiling you're still feeding in an empty string. Also when it comes to inheritance/interfaces there's nothing virtual/signature about it. If declared MyInterface.Foo(string x = null), an implementation can declare MyClass.Foo(string x = "default"), the caller will compile/pass in a value based on the object's known type. - Chris Sinclair

2 Answers

1
votes

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

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

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

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.