Different ways of doing overloading methods
The three ways through which Method overloading can be executed arre:
- By changing the number of parameters.
- By changing the parameters’ data types.
- By changing the parameters’ Order.
By changing the Number of Parameter
C#
// function Overloading by changing the number of parameters
using System;
class Coding Ninjas Studio {
// adding two integer values.
public int func_add(int a, int b)
{
int sum = a + b;
return sum;
}
// adding three integer values.
public int func_add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
public static void Main(String[] args)
{
// Creating the Object
Coding Ninjas Studio ob = new Coding Ninjas Studio();
int sum1 = ob.func_add(1, 2);
Console.WriteLine("sum of the two "
+ "integer value : " + sum1);
int sum2 = ob.func_add(1, 2, 3);
Console.WriteLine("sum of the three "
+ "integer value : " + sum2);
}
}
Output
sum of the two integer value : 3
sum of the three integer value : 6
By changing the Data types of the parameters
C#
using System;
class Coding Ninjas Studio {
// adding three integer values.
public int func_add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
// adding three double values.
public double func_add(double a,
double b, double c)
{
double sum = a + b + c;
return sum;
}
public static void Main(String[] args)
{
// Creating Object
Coding Ninjas Studio ob = new Coding Ninjas Studio();
int sum2 = ob.func_add(1, 2, 3);
Console.WriteLine("sum of the three "
+ "integer value : " + sum2);
double sum3 = ob.func_add(1.0, 2.0, 3.0);
Console.WriteLine("sum of the three "
+ "double value : " + sum3);
}
}
Output
sum of the three integer value : 6
sum of the three double value : 6
By changing the Order of the parameters
C#
using System;
class Coding Ninjas Studio {
// Method
public void Identity(String name, int id)
{
Console.WriteLine("Name1 : " + name + ", "
+ "Id1 : " + id);
}
// Method
public void Identity(int id, String name)
{
Console.WriteLine("Name2 : " + name + ", "
+ "Id2 : " + id);
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
Coding Ninjas Studio obj = new Coding Ninjas Studio();
obj.Identity("Akash", 1);
obj.Identity(2, "John");
}
}
Output
Name1 : Akash, Id1 : 1
Name2 : John, Id2 : 2
Check out this problem - Subarray Sum Divisible By K
Frequently Asked Questions
What is the overloading method in C#?
Method overloading in C# is the ability to define multiple methods with the same name but different parameter lists within the same class.
What is method overriding in C#?
Method overriding in C# allows a subclass to provide a specific implementation of a method already defined in its base class using the override keyword.
What is the method overloading concept?
The method overloading concept allows methods to perform different tasks based on varied parameter lists, improving flexibility and readability within a single class.
Conclusion
In this article, we have extensively discussed Method Overloading in C#. We have also discussed different ways of overloading functions in C#. We hope this blog has helped you to brush up your knowledge regarding the C#. Some official documentation on big data that can help you improve your understanding is C# Generics and C# Inheritance.
If you would like to gain more knowledge about C#, check out our articles on Operator Keyword in C#, cloud platform comparison, and 10 AWS best books. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.