×

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

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes 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# 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# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes 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# 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.

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.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

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# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

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# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.