Lesson 16 Learning to work with the Overloading Method in #C

This is our lesson no 16 and today I will teaches you about Learning to work with the overloading Method in #C.

Optional / defult parameters (( are used in many computer languages. The use of parameters is possible.

When setting, the application can use this technique to specify a default value for Assign one or more function parameters to them. This approach is used to make the code more flexible. The programme is quite beneficial.

For example, suppose you want to offer a function with one or more input parameters certain functionality. In this case, your code may not be performed properly because the required number of parameters were not sent while calling.

You can utilise the new option to specify optional parameters to address this problem. You provided a default value for some arguments in this Method, and even though no value was sent for it when calling Function, the code worked fine.

string Substring (int startIndex)
string Substring (int startIndex, int length)

The above function can be called with one or two parameters. The first example is when you call the function with an argument. The second mode is activated if you call with two parameters.

As a result, by describing alternative shapes for a function, you can greatly reduce the coding volume. To To do this, we create a simple and ordinary method that generates and executes the code of other methods. Consider the following illustration:

class SillyMath
{
public static int Plus(int number1, int number2)
{
return Plus(number1, number2, 0);
}
public static int Plus(int number1, int number2, int number3)
{
return number1 + number2 + number3;
}
}

In the example code above, we have defined the Plus function with two modes. In the first case, the function has two parameters to add 2 Receives the number, while the second case has three parameters.

In fact the main work is done by the 3 parametric version of the function Gives.

If we want to add two numbers, very simply, call the function with 3-parameter mode and the number 1
We pass to the third parameter.

To be used as the default value for it. The example code above, we mean Shows in the previous section.
Now if you want to add up to 4 numbers, you can add another 4 parameter version to the program.
do.

As the following code:

class SillyMath
{
public static int Plus(int number1, int number2)
{
return Plus(number1, number2, 0);
}
public static int Plus(int number1, int number2, int number3)
{
return Plus(number1, number2, number3, 0);
}
public static int Plus(int number1, int number2, int number3, int number4)
{
return number1 + number2 + number3 + number4;
}
}

overloading method #C

The preceding example demonstrates a basic overloading method in C # as well as how to connect functions States.

%d bloggers like this: