← Back to C# Course | Chapter 5: Methods | Lesson 4 of 6

Method Overloading

Method overloading means giving several methods the same name but different inputs, like draw that can draw a circle or a square depending on what you give it. C# picks the right one by looking at what you passed in.

In this page:

  1. Method Overloading
Syntax
csharp
static int MethodName(int a, int b) { }
static double MethodName(double a, double b) { }   // same name, different parameters
static int MethodName(int a, int b, int c) { }

Method Overloading

Overloading lets several methods share the same name as long as their parameter lists differ in type or count. The compiler picks the right overload based on the arguments at the call site.

Overloading is resolved entirely at compile time, not based on runtime values.

Note: Overloads should behave consistently — same name, conceptually the same operation, just different inputs.

Example: Method Overloading

markup
using System;

class Program
{
    static int Add(int a, int b) => a + b;
    static double Add(double a, double b) => a + b;
    static int Add(int a, int b, int c) => a + b + c;

    static void Main(string[] args)
    {
        Console.WriteLine(Add(2, 3));
        Console.WriteLine(Add(2.5, 3.5));
        Console.WriteLine(Add(1, 2, 3));
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Overloading only by return type, which is not allowed.
  2. Creating overloads that make a call ambiguous, such as f(int, double) and f(double, int) called with two ints.
  3. Assuming overload choice happens at runtime, when it is chosen at compile time from the argument types.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.