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

overloading

P: n/a
Hi

I am 'returning' to the learning of C# after a change in jobs and I remember
that I used to struggle with the defintion, purpose and function of
'overloading'. When I was beginning with C# I never fully understood what
overloading was good for.

Could someone please provide me with some words that define overloading,
what it is used for and some really basic examples.

Doug
May 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Doug" <qu********@hotmail.com> wrote:
I used to struggle with the defintion,
Overloading refers to two things:

1) Defining several (i.e >1) methods, constructors, indexers or
operators which differ by signature only, where signature refers to the
number and types of the parameters (aka formal arguments) to the
methods, constructors, indexers or operators.

2) Calling an overloaded method, constructor, indexer or operator and
resolving that call to a specific definition by matching the number and
types of the actual arguments to the signature of the selected method,
constructor, indexer or operator.
purpose and function of 'overloading'.
When I was beginning with C# I never fully understood what
overloading was good for.


Perhaps this can be best described by example: overloading permits a
method to have a single name (and thus a single semantic meaning), yet
have specialized behaviour based on its type.

So, imagine that somebody was annoyed that ICollection uses Count as the
number of items while strings use Length, and they wanted to standardize
their code so they used the same identifier everywhere. One possible way
they could do this is with an overloaded static method:

---8<---
using System;
using System.Collections;

static class Fix
{
public static int GetCount(ICollection c)
{
return c.Count;
}

public static int GetCount(string s)
{
return s.Length;
}
}

static class App
{
static void Main(string[] args)
{
Console.WriteLine(Fix.GetCount(args));
Console.WriteLine(Fix.GetCount(Environment.Command Line));
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
May 21 '06 #2

P: n/a
Probably the best example is in a constructor method of a class.

Since constructors are generally used to initialize the object's default
property values, you could imaging a class that has more than one
constructor, one that takes no arguments (and properties would intialize to
the class designer's default values) and one that takes some arguments (so
certain properties would initialize at the class user's desired values, not
the class designer's desired values).


"Doug" <qu********@hotmail.com> wrote in message
news:44**********************@news.optusnet.com.au ...
Hi

I am 'returning' to the learning of C# after a change in jobs and I
remember that I used to struggle with the defintion, purpose and function
of 'overloading'. When I was beginning with C# I never fully understood
what overloading was good for.

Could someone please provide me with some words that define overloading,
what it is used for and some really basic examples.

Doug

May 21 '06 #3

P: n/a
Doug wrote:
Hi

I am 'returning' to the learning of C# after a change in jobs and I remember
that I used to struggle with the defintion, purpose and function of
'overloading'. When I was beginning with C# I never fully understood what
overloading was good for.

Could someone please provide me with some words that define overloading,
what it is used for and some really basic examples.

Doug


We can look at it from a basic perspective.

Say you needed a function that adds two numbers.

int add(int n1, int n2)
{
return n1 + n2;
}

Later on you find you need an add function that can add to floating
point numbers. You already have a function named add, so what to do?
In C# you can overload the function which mean you have two or more
functions with the same name. The only difference required is that the
functions signature be different. The functions signature is its name
and parameters. So in C# you could do something like:

int add(int n1, int n2)
{
return n1 + n2;
}
double add(double n1, double n2)
{
return n1 + n2;
}

Since the parameter types are different, the compiler can figure out
which one to use by your function call.

int n3 = add(1, 2); // calls the int based func.
double n4 = add(1.2, 2.2); // calls the double based func.

This was a pretty basic description. Google will give you tons of
better examples.

Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
There's no place like 127.0.0.1
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
JimD
Central FL, USA, Earth, Sol
May 21 '06 #4

P: n/a
Another excellent use of overloading is the ability to use default values
for parameters, as in:

public void foo (int x, int y)
{
this.SomeField += x;
this.SomeotherField -= y;
}

public void foo()
{
foo(20, 20);
}

If x and y are usually 20, you can call the function with no parameters. In
exceptional situations, you can pass whatever parameters you need to.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"JimD" <Ji*@keeliegirl.dyndns.org> wrote in message
news:OV*******************@tornado.tampabay.rr.com ...
Doug wrote:
Hi

I am 'returning' to the learning of C# after a change in jobs and I
remember
that I used to struggle with the defintion, purpose and function of
'overloading'. When I was beginning with C# I never fully understood
what
overloading was good for.

Could someone please provide me with some words that define overloading,
what it is used for and some really basic examples.

Doug


We can look at it from a basic perspective.

Say you needed a function that adds two numbers.

int add(int n1, int n2)
{
return n1 + n2;
}

Later on you find you need an add function that can add to floating
point numbers. You already have a function named add, so what to do?
In C# you can overload the function which mean you have two or more
functions with the same name. The only difference required is that the
functions signature be different. The functions signature is its name
and parameters. So in C# you could do something like:

int add(int n1, int n2)
{
return n1 + n2;
}
double add(double n1, double n2)
{
return n1 + n2;
}

Since the parameter types are different, the compiler can figure out
which one to use by your function call.

int n3 = add(1, 2); // calls the int based func.
double n4 = add(1.2, 2.2); // calls the double based func.

This was a pretty basic description. Google will give you tons of
better examples.

Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
There's no place like 127.0.0.1
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
JimD
Central FL, USA, Earth, Sol

May 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.