×

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# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

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...

4 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# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 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# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

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# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 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.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 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# 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.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 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.