overloading

D

Doug

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
 
B

Barry Kelly

Doug said:
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.CommandLine));
}
}
--->8---

-- Barry
 
S

Scott M.

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

JimD

Doug said:
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
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top