×

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give different names to the members of the same meaning and are defined for the same purposes. In C#, we can overload constructors, methods and index properties since they have parameters defined in their signature.
C# Method overloading: In a class, if we defined two methods with the same name but different signature then it's called method overloading. To overload a method we need to:
  1. Change the number of parameters.
  2. Change the data type of parameters.
C# Method overloading example: by changing number of parameters
using System;
public class Calculation
{
            public int addition(int a,int b)
            {
                        return a+b;
            }

            public int addition(int a,int b,int c)
            {
                        return a+b+c;
            }
}
public class OverLoadingExample
{
            public static void Main (string[] args)
            {
                        Calculation cal=new Calculation();
                        Console.WriteLine(cal.addition(10,20));
                        Console.WriteLine(cal.addition(10,20,30));
            }
}
C# Method Overloading Example: by changing data type of parameters
using System;
public class Calculation
{
            public int addition(int a,int b)
            {
                        return a+b;
            }
            public float addition(int a,float b)
            {
                        return a+b;
            }
}
public class OverLoadingExample
{
            public static void Main (string[] args)
            {
                        Calculation cal=new Calculation();
                        Console.WriteLine(cal.addition(10,20));
                        Console.WriteLine(cal.addition(10,20.6f));
            }
}
C# Type Promotion in method overloading: In C#, one data type is promoted to another if no matching is found between function call and function definition. Let's see an example of type promotion in method overloading. The float In the function call is automatically promoted to double of function signature defined.
using System;
public class Calculation
{
            public long addition(int a,long b)
            {
                        return a+b;
            }
            public double addition(int a,double b)
            {
                        return a+b;
            }
}
public class OverLoadingExample
{
            public static void Main (string[] args)
            {
                        Calculation cal=new Calculation();
                        Console.WriteLine(cal.addition(10,20));
                        Console.WriteLine(cal.addition(10,20.6f));
            }
}
Output
30
30.6000003814697

Related Topics

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.