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
// 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
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
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 a class in C#?
A class in C# is a user-defined blueprint for creating objects. It combines many forms of data into a single unit.
What is meant by structure in C#?
A structure in C# is a composite data type made up of numerous data types such as methods, fields, constructors, constants, properties, indexers, operators, and even other structures.
A structure helps in merging different data kinds into a single entity. They are comparable to courses in this regard. On the other hand, structures are value types, whereas classes are reference types.
Illustrate Namespaces in C#?
Namespaces are commonly used to organise huge coding projects. In C#, one of the most popular and commonly used namespaces is "System." One can construct their own namespace and utilise it within another, a process known as nesting.
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.
Do upvote our blog to help other ninjas grow. Happy Coding!