Adding Functions with the Same Name (Method Overloading)


Adding Functions with the Same Name (Method Overloading)

It is possible to add two or more functions with the same name to the same class. This is called method overloading. Programmers normally use method overloading in order to have multiple versions of the same function that accept different numbers of parameters.

To do method overloading:

  1. Add another function with the same name.

  2. Change the type of one of the parameters in the new function ( Figure 6.1 ).

    Figure 6.1 One way to overload functions is to change the type of at least one of the parameters.
     class Account {    public void MakeDeposit(  int  Amount)    {    }    public void MakeDeposit(  double  Amount)    {    } } 

    or

    Change the number of parameters in the new function ( Figure 6.2 ).

    Figure 6.2 Another way to overload functions is to change the number of parameters in the function.
     class Account {    public void MakeDeposit(int Amount)    {    }    public void MakeDeposit(int Amount,  bool Available  )    {    } } 

    or

    Change the direction of one of the parameters ( Figure 6.3 on the next page).

    Figure 6.3 Remember that in C# the caller of the function has to use the word ref in front of any by-ref parameters being sent to the function. In this way the compiler can distinguish between a call to the first version of MakeDeposit (no ref in the call) and the second version (ref in the call).
     class Account {    int Balance=0;    public void MakeDeposit(int Amount)    {       Balance += Amount;    }    public void MakeDeposit(  ref  int Amount)    {      Balance += Amount;      //Amount is a reference parameter      //which means you can change it.      //change it to the actual balance.      Amount = Balance;    } } 
  3. If you make one of the changes in step 2 you can also change the return type of the function ( Figure 6.4 ).

Figure 6.4 You can change the output parameter type only if you also change something in the parameter list.
 class Account {    int Balance=0;    public void MakeDeposit(int Amount)    {      Balance+=Amount;    }    public  int  MakeDeposit(int Amount,  bool Available  )    {        if (Available) Balance+=Amount;        return Balance;    } } 

graphics/tick.gif Tips

  • You can overload functions, constructors, events and indexers (events and indexers are explained in later chapters).

  • You can't overload functions by making one function static and the other an instance function. You must change something in the parameters of the function as well in order to overload the functions.


Which Function Gets Called When?

The difficult part when overloading functions is trying to figure out which function the compiler is going to call. Consider the code in Figure 6.5 . The code in that figure has three functions: one that accepts a double, one that accepts a float, and one that accepts any object. (All types are compatible with the object type.) The code calls MakeDeposit but passes an integer as the parameter. Which of the functions gets called? The rules are described in the C# ECMA Specification under section 14.4.2 Overload Resolution. However, the short version of the rules is that functions are selected based on which function results in a better conversion. Essentially, a better conversion is one where the type you are converting to is closest in memory consumption to the type you are starting from. So in the case of numeric values one can follow the following list: byte, short, int, long, float or decimal, double, object. In this list, byte consumes the least amount of memory and object consumes the most. You pick the smallest type that fits the value you are converting without data loss. In C# all literal integral numbers are of type int by default. However if there is no integer function to invoke, C# checks to see if the number is within the range of a smaller data type (short for example). All decimal numbers are of type double by default. In Figure 6.5 , the closest data type is float.

Figure 6.5 Decisions, decisions. The 500 value is type integer, so which of the MakeDeposit version actually gets called? The answer is double. Double is the closest data type to Integer, memory-wise.
 class Account {    public void MakeDeposit(double Amount)    {    }    public void MakeDeposit(float Amount)    {    }    public void MakeDeposit(object Amount)    {    } } class Bank {    public void CreateAccount()    {       Account acct = new Account();  acct.MakeDeposit(500);  } } 


C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net