Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Method Overloading in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The letter C# is pronounced as "C-Sharp." C# is a Microsoft-developed object-oriented programming language that runs on the.NET Framework. C# is related to other popular languages such as C++ and Java and has roots in the C family.

The first version of C# was released in 2002. C# 8, the most recent version, was published in September 2019. Microsoft's. NET initiative, led by Anders Hejlsberg, developed C#, a simple, modern, general-purpose, object-oriented programming language.

C# Method Overloading

Method Overloading is one of the efficient ways of implementing polymorphism. It's the ability to rewrite a function in multiple ways. A user can implement function overloading by specifying two or more functions in a class with the same name. C# can tell the difference between methods with various signatures. Methods with the same name but various parameter lists (number of parameters, Order of parameters, and data types of parameters) can exist within the same class.

Features Of Method Overloading

  • The number and type of parameters supplied as arguments to overloaded methods are used to distinguish them.
  • There can't be more than one Method with the same name, Order, or type of parameters. It'd be a compiler flaw.
  • When distinguishing the overloaded Method, the compiler ignores the return type. However, two methods with the same signature but different return types cannot be declared. There will be a compilation error. It is not possible if both methods have the same parameter types but distinct return types.

Why do we need Method Overloading?

If we try to define several methods with the same name and number of parameters, the compiler will throw an error. Method overloading has the advantage of making code more readable and maintainable.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction
2.
C# Method Overloading
2.1.
Features Of Method Overloading
2.2.
Why do we need Method Overloading?
3.
Different ways of doing overloading methods
3.1.
By changing the Number of Parameter
3.2.
By changing the Data types of the parameters
3.2.1.
Output
3.3.
By changing the Order of the parameters
3.3.1.
Output
4.
Frequently Asked Questions
4.1.
What is a class in C#?
4.2.
What is meant by structure in C#?
4.3.
Illustrate Namespaces in C#?
5.
Conclusion