471,337 Members | 1,166 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

OVERLOADING

Why use overloading?

I have used it to allow for optional arguments.

It is also possible to use overloading to cater for arguments of different
types, I believe. E.G.

public string mymethod(string myarg)
{
}

public string mymethod(string[] myarg)
{
}

What are the implications of using overloading to cater for arguments of
different types?

Thanks for your help.
Jun 27 '08 #1
1 1059
On Jun 6, 8:00 am, AA2e72E <AA2e...@discussions.microsoft.comwrote:
Why use overloading?
It allows you to reuse a name for many different signatures, which can
increase consistency. Look at Convert.ToInt32 for example - loads of
different overloads. Why would you want to have to specify a different
name for each of those overloads?
I have used it to allow for optional arguments.
Yes, that's a common use.
It is also possible to use overloading to cater for arguments of different
types, I believe. E.G.

public string mymethod(string myarg)
{

}

public string mymethod(string[] myarg)
{

}

What are the implications of using overloading to cater for arguments of
different types?
It's generally okay, *but* you can get into difficulties if there's
any ambiguity. For instance:

public string MyMethod(string x)
....
public string MyMethod(object o)

A string argument is valid for both methods. The C# language spec
specifies how overloads are resolved, but it can get quite hairy,
particularly when inheritance is involved.

If the types are completely distinct (i.e. there are no conversions
between them - e.g. string and float) then there's less of an issue.

Jon
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Terje Slettebø | last post: by
4 posts views Thread by Dave Theese | last post: by
5 posts views Thread by | last post: by
39 posts views Thread by zeus | last post: by
45 posts views Thread by JaSeong Ju | last post: by
31 posts views Thread by | last post: by
15 posts views Thread by lordkain | last post: by
11 posts views Thread by placid | last post: by
10 posts views Thread by Matthew | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.