• Log in or sign up for an account here: https://www.sythe.org/login

[Global]Reported Post by Terrankiller

Status
Not open for further replies.

Terrankiller

Ex-Administrator

Retired Administrator
Visual Basic Programmers
Joined
May 7, 2005
Sythe Gold
1
Messages
1,286
Location
California
Terrankiller has reported a post.

Reason:
Advertisement.
Post: cegonsoft chennai | Method Overloading in C#
Forum: C#
Assigned Moderators: N/A

Posted by: ravbop1
Original Content:
C# supports method overloading, which allows you to create more than one method with the
same name, but with a different set of parameters. When you call the method, the CLR automatically
chooses the correct version by examining the parameters you supply.

This technique allows you to collect different versions of several methods together. For
example, you might allow a database search that returns an array of Product objects representing
records in the database. Rather than create three methods with different names
depending on the criteria, such as GetProducts(), GetCategory(), and
GetPrice(), you could create three versions of the GetPrice() method. Each
method would have the same name but a different signature, meaning it would require different
parameters. This example provides two overloaded versions for the GetPrice()
method:

private decimal GetPrice(int ID)
{
// Code here.
}
private decimal GetPrice(string name)
{
// Code here.
}

// And so on...
Now you can look up product prices based on the unique product ID or the full product
name, depending on whether you supply an integer or string argument:

decimal price;
// Get price by product ID (the first version).
price = GetPrice(2047);
// Get price by product name (the second version).
price = GetPrice("Air conditioner");

Note : You cannot overload a method with versions that have the same signature—that is, the
same number of parameters and parameter data types—because the CLR will not be able to
distinguish them from each other. When you call an overloaded method, the version that
matches the parameter list you supply is used. If no version matches, an error occurs.

Visit: Cegonsoft
 
Status
Not open for further replies.
Runescape Gamblingrunescape gambling
Back
Top