// Overloaded Add() method.
static int Add(int x, int y)
{ return x + y; }
static double Add(double x, double y)
{ return x + y; }
static long Add(long x, long y)
{ return x + y; }
}
■
Note
As explained in Chapter 10, it is possible to build generic methods that take the concept of overloading to
the next level. Using generics, you can define “placeholders” for a method implementation that are specified at the
time you invoke the member.
The caller can now simply invoke Add() with the required arguments and the compiler is happy
to comply, given the fact that the compiler is able to resolve the correct implementation to invoke
given the provided arguments:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Method Overloading *****");
C H A P T E R 4
■
C O R E C # P R O G R A M M I N G C O N S T R U C T S, PA RT I I
113
8849CH04.qxd 10/1/07 10:31 AM Page 113
Do'stlaringiz bilan baham: |