Understanding Method Overloading and Signatures

Understanding Method Overloading and Signatures

Method overloading is an important function method concept in the C# programming language. It allows C# programmers to define or create multiple user-defined class methods with the same name but with separate variable parameter lists. Method overloading in C# allows a single class method name to handle multiple individual class inputs or different numbers of user-defined program variable parameters within a single function by allowing the function to handle multiple individual class inputs or different numbers of user-defined program variable parameters.

Understanding Method Overloading and Signatures

So, let’s explore the multiple parameter variable function method overloading concept in C# programming.

What is a method signature in C# programming?

A method signature in a C# program refers to the declaration of multiple combinations of parameter variables in a user-defined class program, which includes the name of a class method, multiple individually declared class variables, and the parameter data type, which includes the number of class parameters, the declared parameter data types, and their declaration sequence or order.

The return data type and class parameter names of a program method are not part of the method signature.

Example of a method signature in a C# program.

public int Total(int p, int q) { } // Here, a method named “Total” is defined or declared in the class program, with a variable parameter list of the int data type “(int p, int q)”, which indicates a method signature.

Remember, a class method signature declared in a C# program must be unique and distinct within the class. You cannot declare two class methods with the same name and the same program variable parameter data type in a C# program.

Method Overloading in C# Programming.

The method overloading concept in a C# program allows a C# programmer to create multiple new class methods with the same name but separate individual multiple class variable parameter lists. This means that methods declared in the same class program must differ in one of the following ways.

Element of Method Overloading.

  • The number of variable parameters declared in a class, for example, one method declared holds one parameter value, similarly the second method holds two parameter values.
  • The data type of variable parameters declared in a class, for example, one method declared in a class program holds an int data type value, while the second parameter holds or deals with a string value.
  • The sequence or order of variable parameters declared in a class, for example, one class method declares an int data type, and the second parameter deals with a string value, and the second data type deals with both string and int values.
  • Remember, the signatures of the functions declared and overloaded methods in a class program must be unique and individual.

Example of method overloading in a C# program.

C# program using the overloading concept based on the number of variable parameters declared in a class method.

using System;

class NumOfParameterOverlod

{

static void Main()

{

Console.WriteLine(total(2, 9)); // here it calls the total class method (int, int) with 2 integer data type values

Console.WriteLine(total(7, 4, 9, 3)); // Here it calls total(int, int, int, int) class method parameter with 4 different int data type values.

}

// Here we declare a class method with 2 int data type parameters.

static int total(int p, int q)

{

return p + q;

}

// Here we overloaded a class method with 4 different integer data type parameters.

static int total(int p, int q, int r, int s)

{

return p + q + r + s;

}

}

Explaining method overloading.

  • In this program, both class methods are defined with the name total, but the class method declared with 4 variable parameters is treated as a separate method. Due to the different number of variable parameters declared in the existing program, it acts as a separate method source code.

Overloading example based on parameter type.

Using System;

class LoadedParameter

{

static void Main()

{

Console.WriteLine(Total(6, 9)); // here it Calls Total(int, int) 2 integer data type parameter value

Console.WriteLine(Total(2.9, 7.7, 3.8)); // here it Calls Total(double, double, double) 3 double data type parameter value

}

// here we declare class Method with 2 integer data type parameters

static int Total(int p, int q)

{

return p + q;

}

// here we use class Loaded method with 3 double data type parameters

static double Total(double p, double q, double r)

{

return p + q + r;

}

}

Explaining Overloading based on parameter type.

  • This program defines two different class methods. The first method, Total(int, int), handles integer value data types. Similarly, the class method, Total(double, double, double), handles double data types. Here, the compiler can differentiate between multiple methods based on the data type of the class method argument values ​​passed.

Example of method overloading based on parameter order in a C# program.

using System;

class ParameterOverloaded

{

static void Main()

{

Console.WriteLine(Group(“Vcanhelpsu”, 207)); // here it calls the Group(string, int) class method with two different data types.

Console.WriteLine(Group(1, “C# Programming”)); // Here it calls Group(int, string) for two class methods with int and string data types.

}

// Here we declare two data type class methods with string and int variable parameters.

static string Group(string p, int q)

{

return p + q;

}

// Here we overloaded a class method with two int and string data type parameters.

static string Group(int p, string q)

{

return p + ” | ” + q;

}

}

Explaining method overloading based on parameter order.

  • In this program, the Group user-defined method is overloaded to manage or handle the order of two individual class variable parameters. In this case, it manages a (string, int) string and a brick data type, while in the other case, it manages or controls a (int, string) brick and a string data type.

Rules for method overloading in C# programming.

  • If you want to apply the method overloading concept in a C# program, you must follow these rules and regulations.
  • Class method name – The overloaded class method must have the same name in the C# program.
    • Different parameter list – A class method similar to the existing C# program with different variable parameter values ​​must have at least one of the following differences:
    • The number of parameters in the class.
    • The data type of multiple declared variable parameters.
    • The sequence or order of declared variable parameters in the class.
  • Method return type – The return data type of a declared class method in the existing program does not make any user-defined class method signature unique. Therefore, you cannot overload a class method by simply modifying the return class data type.

The program source code below will not compile for a class method example. Because the user-declared class method signatures in this program are defined identically.

public int Total(int p, int q) { return p + q; }

public double Total(int a, int q) { return a + q; } // This displays an error message because the class has the same signature, including return type information.

Overloading with Default Parameters in C#.

In a C# program, programmers can overload any class method by using the default class-declared variable parameter value. This process gives the class method the behavior of handling individual number parameters without explicitly overloading them.

Example of a method with default parameters in a C# program.

using System;

class DefaultParameterMethod

{

static void Main()

{

Console.WriteLine(Total(2, 7)); // here it Calls Total(int, int) class method with 2 integer data type value

Console.WriteLine(Total(4, 7, 4)); // here it Calls Total(int, int, int) class method with 3 integer data type value

Console.WriteLine(Total(11)); // here it Calls Total(int, int) with a default parameter for second class parameter value

}

// here we define class Method with default parameter variable value

static int Total(int p, int q = 0)

{

return p + q;

}

//here we overloaded class method with 3 variable parameters value

static int Total(int p, int q, int r)

{

return p + q + r;

}

}

Explaining Overloading with Default Parameters.

  • In this program, the class method Total(int p, int q = 0) defines a default value of 0 for the variable parameter q. If the method caller doesn’t pass a value for q, it will default to 0. This will call the existing class method as if it had only one argument, which is similar to class overloading.

C# Method Overloading vs. Method Overriding Concepts.

  • In C# programming, it’s important to understand the difference between method overloading and method overriding concepts.
  • In C#, method overloading behavior is defined within a user-defined class, where C# users can define or create multiple class methods with the same name. However, the signatures of these similar classes must be distinct or separate.
  • In C# programs, method overriding is used in inheritance to provide a new implementation of a class method already defined in a base class. This is done using the virtual keyword and the override modifier.

A Summary of Understanding Method Overloading and Signatures.

  • Method overloading in the C# programming language allows users to define or create multiple class methods with the same name. However, the class method’s variable parameter lists must be distinct, such as the number of parameters, data types, or the order or sequence of declared parameters.
  • In a C# program, the method signature contains the class method name and its declared parameter list, such as the data type, number of parameters, and their declared sequence or order.
  • Method overloading in a C# program is based only on the user-declared variable or parameter list, not on the nature of any return data type.
  • Default class parameters in a C# program can provide functionality similar to method overloading by allowing multiple class methods to handle or manage different numbers of variable parameters.

Leave a Reply