|
|
28 Module 5: Methods and Parameters
Guidelines for Passing Parameters
n Mechanisms
l Pass by value is most common
l Method return value is useful for single values
l Use ref and/or out for multiple return values
l Only use ref if data is transferred both ways
n Efficiency
l Pass by value is generally the most efficient
With so many options available for parameter passing, the most appropriate
choice might not be obvious. Two factors for you to consider when you choose
a way to pass parameters are the mechanism and its efficiency.
Mechanisms
Value parameters offe
Module 5: Methods and Parameters 29
Using Recursive Methods
n A Method Can Call Itself
l Directly
l Indirectly
n Useful for Solving Certain Problems
n Example
A method can call itself. This technique is known as recursion. You can
address some types of problems with recursive solutions. Recursive methods
are often useful when manipulating more complex data structures such as lists
and trees.
Methods in C# can be mutually recursive. For example, a situation in which
method A can call method B, and method B can call method A, is allowable.
Example of a Recursive Method
The Fibonacci sequence occurs in several situations in mathematics and biology
(for example, the reproductive rate and population of rabbits). The nth member
of this sequence has the value 1 if n is 1 or 2; otherwise, it is equal to the sum of
the preceding two numbers in the sequence. Notice that when n is greater than
two the value of the nth member of the sequence is derived from the values of
two previous values of the sequence. When the definition of a method refers to
the method itself, recursion might be involved.
You can implement the Fibonacci method as follows:
static ulong Fibonacci(ulong n)
{
if (n <= 2)
return 1;
else
return Fibonacci(n-1) + Fibonacci(n-2);
}
Notice that two calls are made to the method from within the method itself.
A recursive method must have a terminating condition that ensures that it will
return without making further calls. In the case of the Fibonacci method, the
test for n <= 2 is the terminating condition.
30 Module 5: Methods and Parameters
u Using Overloaded Methods
n Declaring Overloaded Methods
n Method Signatures
n Using Overloaded Methods
Methods might not have the same name as other non-method items in a class.
However, it is possible for two or more methods in a class to share the same
name. Name sharing among methods is called overloading.
In this section, you will learn:
n How to declare overloaded methods.
n How C# uses signatures to distinguish methods that have the same name.
n When to use overloaded methods.
Module 5: Methods and Parameters 31
Declaring Overloaded Methods
n Methods That Share a Name in a Class
l Distinguished by examining parameter lists
class OverloadingExample
{
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Main( )
{
Console.WriteLine(Add(1,2) + Add(1,2,3));
}
}
class OverloadingExample
{
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Main( )
{
Console.WriteLine(Add(1,2) + Add(1,2,3));
}
}
Overloaded methods are methods in a single class that have the same name. The
C# compiler distinguishes overloaded methods by comparing the parameter
lists.
Examples of Overloaded Methods
The following code shows how you can use different methods with the same
name in one class:
class OverloadingExample
{
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Main( )
{
Console.WriteLine(Add(1,2) + Add(3,4,5));
}
} |
|