Method Overloading
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Method Overloading
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Multiple threads modifying shared buffer [Producer/Consumer Relationship] [next Lesson]  Web.config Tutorial
Method Overloading
  by: narendra

Method Overloading

by: narendra

What is Method Overloading?

C# allows you to define different versions of a method in class, and the compiler will automatically select the most appropriate one based on the parameters supplied.

Declearing Oparetor overloading

While display a integer we can write:
int x = 10;
Console.WriteLine (x);
While display a integer we can write:
string Message = "Hello";
Console.WriteLine (Message);
How is this possible? what parameter type does Console.WriteLine () take? If it was expecting to take a string, then the first of these two examples would give error, because there is no implicit cast from int to string.

If Console.WriteLine () was expecting to take any numaric dat type however, then the second of these examples would give a compilation error there is no implicit cast from string to any numaric type. Yet fact, both of these lines will compile without error and run to give the expected results.

The reason is that there are two diffrent Console.writeLine( ) methods - one of them takes an int as parameter, the other one takes a string.

add Method Overloading?

In addition of two or three integers, we add two integer values as a method, add three integer values as a method, as well. i.e:
public class AddingNumbers
{
    public int Add(int a, int b)
    {
        return a+b;
    }

    public int Add(int a, int b, int c)
    {
        return a+b+c;
    }
     
}
Calling Overloaded Methods?
public int add(int x, int y)
public int add(int x, int y, int z)
We've also assumed that two overloaded methods.

Overload 1 requires two integers - exactly the parameters that we are passing in. The compiler will generate code that calls this overload.

Overload 2 requires three integers.

When to use Method Overloading?

Generally, you should consider overloading a method when you for some reason need a couple of methods that take different perameters, but conceptually do the same thing. Console.WriteLine( ) is a good example. Another good example method is a method Sign( ) in the System.Math base class, which returns the sign of a number, and is overloaded to work out the sign of double, a decimal and other signed types.

In general, you should not use overloads when two methods really do diffrent things - otherwise you'll simply confuse the developers who have to use your classes.

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Method Overloading
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Multiple threads modifying shared buffer [Producer/Consumer Relationship] [next Lesson]  Web.config Tutorial


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
simboy 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center