0
0
C Sharp (C#)programming~10 mins

Method overloading in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method overloading
Call method with arguments
Check method name
Match argument types/count
Call matched method
Execute method
Return result
When you call a method, the program looks for the same method name with matching argument types and count, then runs that specific version.
Execution Sample
C Sharp (C#)
class Calculator {
  public int Add(int a, int b) {
    return a + b;
  }
  public int Add(int a, int b, int c) {
    return a + b + c;
  }
}

Calculator calc = new Calculator();
int result1 = calc.Add(2, 3);
int result2 = calc.Add(1, 4, 5);
This code shows two Add methods with different numbers of parameters. The program chooses which Add to run based on how many numbers you give.
Execution Table
StepMethod CalledArgumentsMatch Found?ActionOutput
1Add(2, 3)Yes (Add(int, int))Execute Add(int, int)5
2Add(1, 4, 5)Yes (Add(int, int, int))Execute Add(int, int, int)10
3Add(2)NoError: No matching methodNone
💡 Execution stops after matching method runs or error if no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
result1undefined555
result2undefinedundefined1010
Key Moments - 2 Insights
Why does the program choose the Add method with two parameters when I call Add(2, 3)?
Because in the execution_table row 1, the method Add(int, int) matches exactly the two arguments (2, 3), so it runs that version.
What happens if I call Add with one number like Add(2)?
As shown in execution_table row 3, no Add method matches one argument, so the program gives an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when calling Add(1, 4, 5)?
A10
B5
CError
DNone
💡 Hint
Check execution_table row 2 for the output of Add(1, 4, 5).
At which step does the program fail to find a matching method?
AStep 1
BStep 2
CStep 3
DNo failure
💡 Hint
Look at execution_table row 3 where no match is found.
If you add a method Add(int a), what would happen when calling Add(2)?
AIt will still give an error
BIt will call Add(int a) and return 2
CIt will call Add(int, int)
DIt will call Add(int, int, int)
💡 Hint
Refer to the concept_flow where matching argument count and types decides the method.
Concept Snapshot
Method overloading means having multiple methods with the same name but different parameters.
When you call the method, the program picks the one matching the number and types of arguments.
If no match is found, you get an error.
This helps write cleaner code with similar actions but different inputs.
Full Transcript
Method overloading allows a class to have multiple methods with the same name but different parameters. When you call a method, the program checks the method name and looks for a version with matching argument types and count. If it finds one, it runs that method. If not, it gives an error. For example, a Calculator class can have Add methods with two or three numbers. Calling Add(2, 3) runs the two-parameter version and returns 5. Calling Add(1, 4, 5) runs the three-parameter version and returns 10. Calling Add(2) causes an error because no matching method exists. This feature helps keep code simple and organized when similar actions need different inputs.