Книга: C# 2008 Programmer

Overloading Methods

Overloading Methods

When you have multiple methods in a class having the same name but different signatures (parameters), they are known as overloaded methods. Consider the following class definition:

public class BaseClass {
 public void Method(int num) {
  Console.WriteLine("Number in BaseClass is " + num);
 }
 public void Method(string st) {
  Console.WriteLine("String in BaseClass is " + st);
 }
}

Here, BaseClass has two methods called Method() with two different signatures — one integer and another one string.

When you create an instance of BaseClass, you can call Method() with either an integer or string argument and the compiler will automatically invoke the appropriate method:

BaseClass b = new BaseClass();
//---prints out: Number in BaseClass is 5---
b.Method(5);
//---prints out: String in BaseClass is This is a string---
b.Method("This is a string");

Suppose that you have another class inheriting from BaseClass with a Method() method that has a different signature, like this:

public class DerivedClass : BaseClass {
 //---overloads the method---
 public void Method(char ch) {
  Console.WriteLine("Character in DerivedClass is " + ch);
 }
}

Then, DerivedClass now has three overloaded Method() methods, as illustrated in Figure 6-3.


Figure 6-3 

You can now pass three different types of arguments into Method() — character, integer, and string:

DerivedClass d = new DerivedClass();
//---prints out: Character in DerivedClass is C--- 
d.Method('C');
//---prints out: Number in BaseClass is 5---
d.Method(5);
//---prints out: String in BaseClass is This is a string---
d.Method("This is a string");

What happens if you have a Method() having the same signature as another one in the base class, such as the following?

public class DerivedClass : BaseClass {
 //---overloads the method with the same parameter list---
 public void Method(int num) {
  Console.WriteLine("Number in DerivedClass is " + num);
 }
 //---overloads the method
 public void Method(char ch) {
  Console.WriteLine("Character in DerivedClass is " + ch);
 }
}

In this case, Method(int num) in DerivedClass will hide the same method in BaseClass, as the following printout proves:

DerivedClass d = new DerivedClass();
//---prints out: Number in DerivedClass is 5---
d.Method(5);
//---prints out: String in BaseClass is This is a string---
d.Method("This is a string");
//---prints out: Character in DerivedClass is C---
d.Method('C');

If hiding Method(int num) in BaseClass is your true intention, use the new keyword to denote that as follows (or else the compiler will issue a warning):

//---overloads the method with the same parameter list
public new void Method(int num) {
 Console.WriteLine("Number in DerivedClass is " + num);
}

In C#, you use the new keyword to hide methods in the base class by signature. C# does not support hiding methods by name as is possible in VB.NET by using the Shadows keyword.

The following table summarizes the different keywords used for inheritance.

Modifier Description
new Hides an inherited method with the same signature.
static A member that belongs to the type itself and not to a specific object.
virtual A method that can be overridden by a derived class.
abstract Provides the signature of a method/class but does not contain any implementation.
override Overrides an inherited virtual or abstract method.
sealed A method that cannot be overridden by derived classes; a class that cannot be inherited by other classes.
extern An "extern" method is one in which the implementation is provided elsewhere and is most commonly used to provide definitions for methods invoked using .NET interop.

Оглавление книги


Генерация: 1.048. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз