Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering Method Overloading in C#: Concepts, Examples, and Best Practices

Mastering Method Overloading in C#

Discover how to overload methods in C#, including changing parameter counts, types, and order, with clear examples.

In C#, a class can contain multiple methods with the same name but distinct parameter lists—this is called method overloading. For example:

void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }

Here, the display() method is overloaded. These methods share the same name but accept different arguments.

Note: Overloading is determined solely by parameter lists; return types may differ.


1. By Changing the Number of Parameters

You can overload a method if the number of parameters differs.

void display(int a) {
  ...
}
...
void display(int a, int b) {
  ...
}

Example:

using System;

namespace MethodOverload {

  class Program {

    // method with one parameter
    void display(int a) {
      Console.WriteLine("Arguments: " + a);
    }
 
    // method with two parameters
    void display(int a, int b) {
      Console.WriteLine("Arguments: " + a + " and " + b);
    }
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display(100, 200);
      Console.ReadLine();
    }
  }
}

Output

Arguments: 100
Arguments: 100 and 200

Based on the number of arguments passed, the appropriate overload is invoked.


2. By Changing the Data Types of Parameters

Overloading also works when the parameter types differ, even if the count is the same.

void display(int a) { ... }
...
void display(string b) { ... }

Example:

using System;

namespace MethodOverload {

  class Program {

    // method with int parameter
    void display(int a) {
      Console.WriteLine("int type: " + a);
    }
 
    // method with string parameter
    void display(string b) {
      Console.WriteLine("string type: " + b);
    }
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display("Programiz");
      Console.ReadLine();
    }
  }
}

Output

int type: 100
string type: Programiz

3. By Changing the Order of Parameters

When the parameter list has the same types and count, changing their order creates distinct overloads.

void display(int a, string b) { ... }
...
void display(string b, int a) { ... }

Example:

using System;

namespace MethodOverload {

  class Program {

    // method with int and string parameters
    void display(int a, string b) {
      Console.WriteLine("int: " + a);
      Console.WriteLine("string: " + b);
    }
 
    // method with string and int parameters
    void display(string b, int a) {
      Console.WriteLine("string: " + b);
      Console.WriteLine("int: " + a);
    }
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100, "Programming");
      p1.display("Programiz", 400);
      Console.ReadLine();
    }
  }
}

Output

int: 100
string: Programming
string: Programiz
int: 400

C Language

  1. C# Methods Explained: Declaration, Calling, Parameters, Return Types & More
  2. Mastering C# Abstract Classes & Methods: A Practical Guide
  3. Understanding C# Partial Classes and Methods
  4. Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.
  5. Mastering Constructor Overloading in C#
  6. C++ Function Overloading: A Practical Guide
  7. Master C++ Operator Overloading: Practical Examples & Best Practices
  8. C# Methods: Defining, Calling, and Using Functions
  9. Mastering Operator Overloading in C# for Custom Types
  10. C# Anonymous Methods: Simplifying Delegate Usage