C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will explore method overloading in C#, a powerful feature of object-oriented programming that allows you to create multiple methods with the same name but different parameters. Method overloading improves code readability and flexibility, making it easier to implement different behaviors based on the input arguments.
To overload a method in C#, follow these rules:
Consider a simple Calculator
class that can add integers or doubles:
public class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } }
In this example, the Calculator
class has two overloaded Add
methods. One method takes two integers as parameters and returns their sum, while the other takes two doubles and returns their sum.
To use overloaded methods, simply call the method with the appropriate arguments:
Calculator calculator = new Calculator(); int intResult = calculator.Add(3, 5); // Calls the Add method that takes two integers Console.WriteLine(intResult); // Output: 8 double doubleResult = calculator.Add(3.5, 4.2); // Calls the Add method that takes two doubles Console.WriteLine(doubleResult); // Output: 7.7
In this example, the appropriate Add
method is called based on the types of the input arguments.
You can also overload methods with optional parameters and params
arrays:
public class Calculator { public int Multiply(int a, int b) { return a * b; } public int Multiply(int a, int b, int c = 1) { return a * b * c; } public int Multiply(params int[] numbers) { int product = 1; foreach (var number in numbers) { product *= number; } return product; } }
In this example, the Multiply
method is overloaded with optional parameters and a params
array. When calling the method, the appropriate version will be selected based on the number of input arguments.
Calculator calculator = new Calculator(); int result1 = calculator.Multiply(2, 3); // Calls the Multiply method with two parameters Console.WriteLine(result1); // Output: 6 int result2 = calculator.Multiply(2, 3, 4); // Calls the Multiply method with three parameters Console.WriteLine(result2); // Output: 24 int result3 = calculator.Multiply(1, 2, 3, 4, 5); // Calls the Multiply method with a params array Console.WriteLine(result3); // Output: 120
This tutorial demonstrates method overloading in C#, which allows you to create multiple methods with the same name but different parameters. Method overloading is an essential concept in object-oriented programming, making it easier to implement different behaviors based on input arguments and improving code readability and flexibility.
How to use method overloading in C#
Method overloading in C# allows defining multiple methods in the same class with the same name but different parameter lists.
class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } }
C# method overloading examples
class Example { public void Display(int number) { Console.WriteLine($"Displaying integer: {number}"); } public void Display(double number) { Console.WriteLine($"Displaying double: {number}"); } }
C# method overloading with different parameter types
class Example { public void Display(int number) { Console.WriteLine($"Displaying integer: {number}"); } public void Display(string text) { Console.WriteLine($"Displaying string: {text}"); } }
Overloading constructors in C#
class Person { public string Name { get; } // Constructor overloading public Person(string name) { Name = name; } public Person(string firstName, string lastName) { Name = $"{firstName} {lastName}"; } }
Static method overloading in C#
class MathOperations { public static int Add(int a, int b) { return a + b; } public static double Add(double a, double b) { return a + b; } }
C# method overloading and optional parameters
class Printer { // Overloaded method with optional parameter public void Print(string text, bool bold = false) { Console.WriteLine(bold ? $"**{text}**" : text); } }
Using params keyword in method overloading in C#
class MathOperations { // Overloaded method with params keyword public int Add(params int[] numbers) { return numbers.Sum(); } }
C# method overloading with named parameters
class Shape { // Overloaded method with named parameters public void Draw(int width, int height) { Console.WriteLine($"Drawing shape with width: {width}, height: {height}"); } public void Draw(int size, bool isSquare = false) { Console.WriteLine(isSquare ? $"Drawing square with size: {size}" : $"Drawing rectangle with size: {size}"); } }
Overloading methods in C# interfaces
Interface methods can be overloaded in implementing classes:
interface IShape { void Draw(int width, int height); void Draw(int size, bool isSquare); }
C# method overloading and type promotion
C# performs implicit type promotion when choosing the appropriate overloaded method. For example, an int
argument may be promoted to double
if needed.