By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,855 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,855 IT Pros & Developers. It's quick & easy.

what is the use of over loaded functions.

P: n/a
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#

Jul 30 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
<ve********************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Instead of using same name i can use different name,
True, but then it's not the same function...:-)
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with optional
parameters, but C# doesn't, thank God!

http://www.csharpfriends.com/Article...?articleID=105
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #2

P: n/a
On Jul 30, 12:53 pm, veebhudhi.chandramo...@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
For constructors, you can't change the name to start with.
For everything else - it's just more convenient not to have to create
lots of different method names.

Look at the overloads for Console.WriteLine, for example - would you
really want that many different method names?

Jon

Jul 30 '07 #3

P: n/a
One use of overloaded is to provide default parameter values.

Consider a function that takes three parameters with one parameter being
the same for 90% of the calls.
You could overload that function with one that takes two parameters and
delegate the call to your original function providing the third value
yourself.

My 2c.

ve********************@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
--
Hitesh Ashar
hitesh (at) asharism (dot) com
Jul 30 '07 #4

P: n/a
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???
Jul 30 '07 #5

P: n/a
"Paul Werkowitz" <ne********@primaprogramm.dewrote in message
news:1t*******************************@40tude.net. ..
void f( int data, bool withLogging = false )
That isn't an optional parameter - it's a parameter with a default value...

Not the same thing at all!
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #6

P: n/a
On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
void f( int data, bool withLogging = false )

That isn't an optional parameter - it's a parameter with a default value...

Not the same thing at all!
I certainly see the two as being equivalent. There's no point in
having a default value unless the parameter is optional, and if it's
optional then surely it needs a default value.

What's the practical difference between the two in your view?

Jon

Jul 30 '07 #7

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
void f( int data, bool withLogging = false )

That isn't an optional parameter - it's a parameter with a default
value...

Not the same thing at all!

I certainly see the two as being equivalent. There's no point in
having a default value unless the parameter is optional, and if it's
optional then surely it needs a default value.

What's the practical difference between the two in your view?
There's no practical difference, but an optional parameter (certainly in the
VB utilisation) doesn't actually have to exist...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #8

P: n/a
On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
What's the practical difference between the two in your view?

There's no practical difference, but an optional parameter (certainly in the
VB utilisation) doesn't actually have to exist...
What exactly does that mean? What does a parameter which doesn't exist
look like?

Jon

Jul 30 '07 #9

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
>>What's the practical difference between the two in your view?

There's no practical difference, but an optional parameter (certainly in
the
VB utilisation) doesn't actually have to exist...

What exactly does that mean? What does a parameter which doesn't exist
look like?
Private Sub Draw(Optional X As Variant, Optional Y As Variant)
If IsMissing(X) Then X = 720
If IsMissing(Y) Then Y = 2880
Cls
Circle (X, Y), 700
End Sub

Draw()
Draw(600)
Draw(600, 1500)
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #10

P: n/a
On Jul 30, 4:22 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
What exactly does that mean? What does a parameter which doesn't exist
look like?

Private Sub Draw(Optional X As Variant, Optional Y As Variant)
If IsMissing(X) Then X = 720
If IsMissing(Y) Then Y = 2880
Cls
Circle (X, Y), 700
End Sub

Draw()
Draw(600)
Draw(600, 1500)
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is? Does it work
for types other than Variant?

Jon

Jul 30 '07 #11

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?
I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)
Does it work for types other than Variant?
It didn't in VB...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #12

P: n/a
On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?

I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)
Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>

So at least in VB.NET, they *are* the same thing after all. That's a
relief in terms of my mental model, at the very least :)
Does it work for types other than Variant?

It didn't in VB...
I suspect that it was just a "default default" value which was the
equivalent of Nothing, or some singleton "missing value".

Jon

Jul 30 '07 #13

P: n/a
Jon Skeet [C# MVP] wrote:
On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
>>And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?
I have no idea - that's actually an old bit of VB code - no idea at all if
it would work in VB.NET... :-)

Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>
Yes, in fact "optional parameter" is a misleading term. I remember a
long thread back around 2000 in this ng, back when C# was new and some
people had a real issue with C# not having VB's concept of a so-called
"optional parameter" (and, indeed, VB.NET not supporting optional
parameters a la VB6). They aren't optional parameters but are hard-coded
default values.

IIRC, they are not fun to use because they are like a C# public const
variable vs. a readonly variable. With the former, the const value is
actually pulled out of the referenced assembly and inserted directly
into the consuming assembly; with the latter, a reference to the
readonly variable is deposited into the consuming assembly. Why is this
important. Well if you set your const to 12 and then change it to 13
then each and every assembly that references that public const must then
be recompiled to get the new 13 value. VB.NET's so-called optional
parameters work the same way. Avoid them! :-)

Here's what I could find with Google.

Real optional params (i.e., much of what has been discussed again in
this thread and real optional parameters with C#'s params keyword):

http://groups.google.com/group/micro...e95c94bf945e2d

Pointed to from a thread in this ng - calling COM objects that have
optional parameters:

http://support.microsoft.com/default...b;en-us;305814

And I joined in the fray here:

http://groups.google.com/group/micro...fa58605ebd935e

Lastly, another thread I just read points out that VB.NET optional
parameters are not CLS-compliant so, for example, an optional parameter
to a VB.NET method would only be optional to other VB.NET code, to C#
and other non-VB.NET languages the parameter would not be optional.

HTH.
--
-glenn-
Jul 30 '07 #14

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
And what does that compile to in IL? Is "IsMissing" effectively the
same as "IsNothing" or whatever the VB terminology is?

I have no idea - that's actually an old bit of VB code - no idea at all
if
it would work in VB.NET... :-)

Ah. Looking in MSDN for VB.NET, it specifies:

<quote>
Every optional parameter in the procedure definition must specify a
default value.
</quote>

So at least in VB.NET, they *are* the same thing after all. That's a
relief in terms of my mental model, at the very least :)
I couldn't agree more! I always hated that whole VB "sloppiness"...
Does it work for types other than Variant?

It didn't in VB...

I suspect that it was just a "default default" value which was the
equivalent of Nothing, or some singleton "missing value".
No doubt.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #15

P: n/a
"G.Doten" <gd****@gmail.comwrote in message
news:Og**************@TK2MSFTNGP04.phx.gbl...
HTH.
Extremely interesting - thanks...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #16

P: n/a
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )
And what about
void f(int data)
void f(int data, bool withLogging)
void f(int data, string loggingSource)

There are many similar examples in the CRL

Christof
Jul 31 '07 #17

P: n/a
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>
It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???
Another Point:
In your second case the callee can't react on the missing parameter in a
more dynamic way, wich is possible with overloads aswell as with the VB
style optional parameters without default values.

Christof
Jul 31 '07 #18

P: n/a
Christof Nordiek wrote:
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
>Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )
And what about
void f(int data)
void f(int data, bool withLogging)
void f(int data, string loggingSource)

There are many similar examples in the CRL

Christof

Nothing wrong with doing that!

The pattern I use for this type of situation is like this:

public void f(int data)
: this(data, false, null) {}

public void f(int data, bool withLogging)
: this(data, withLogging, null) {}

public void f(int data, string loggingSource)
: this(data, true, loggingSource) {}

public void f(int data, bool withLogging, string loggingSource) {}

These are all basically convenience overloads for the caller.

The first one means "call f and don't do any logging at all."

The second one means "call f and log or don't depending on the
withLogging parameter's value; if withLogging is true log to the default
log source."

The third is "call f and log or don't log depending on the withLogging
parameter's value, and further if withLogging is true then log to the
source specified in loggingSource (unless the caller explicitly
specified null in which case log to the default log source)."

And sometimes the 4th one is private if I don't want to expose that
variant of the constructor. I also use this same pattern with method
overloads quite frequently as well.

There is a bit of an art to coming up with good, useful overloads and
not just overloading for the sake of it.

--
-glenn-
Jul 31 '07 #19

P: n/a
Christof Nordiek wrote:
"Paul Werkowitz" <ne********@primaprogramm.deschrieb im Newsbeitrag
news:1t*******************************@40tude.net. ..
>Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
>>It allows you to define different "versions" of the same function with
different signatures - other languages did something similar with
optional
parameters, but C# doesn't, thank God!
Optional parameters:
Why do you prefer

void f ( int data )
void f ( int data, bool withLogging )
over

void f( int data, bool withLogging = false )

???

Another Point:
In your second case the callee can't react on the missing parameter in a
more dynamic way, wich is possible with overloads aswell as with the VB
style optional parameters without default values.

Christof

First off, VB.NET doesn't have optional parameters without defaults,
they must always have defaults. Are you referring to the optional
parameters as implemented by VB6 and earlier?

In either case, can you elaborate on what you mean by "can't react on
the missing parameter in a more dynamic way," since there are no missing
parameters in the f example, and I don't get what you mean. Thanks!

--
-glenn-
Jul 31 '07 #20

P: n/a
Dom
On Jul 30, 7:53 am, veebhudhi.chandramo...@gmail.com wrote:
Instead of using same name i can use different name,
is there any specific purpous is there to define the polymorphic
overloaded functions in c#
For the record, you shouldn't use the phrase "polymorphic overloaded
functions". You are confusing two very different concepts,
polymorphism (which is necessary for an Object Oriented language), and
overloading (which is not).

You are right to say that overloading just lets you reuse a name, but
that is actually a fairly big advantage. In my old job, we had a
library of reusable routines that anyone could contribute to. I
remember some of the important routines: Sort (which sorted an
integer array), InSort2 (which sorted a short array), CHSort (which
sorted strings), SortB (which sorted bytes), etc. It got messy. It
is much nicer now with overloading, and we can call of them "Sort".

Jul 31 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.