What is overloading in C#?

WBOY
Release: 2023-09-06 22:17:02
forward
842 people have browsed it

What is overloading in C#?

C# provides two techniques to achieve static polymorphism -

  • Function overloading
  • Operator overloading

Function overloading

Two or more methods with the same name but different parameters are our function overloading in C#.

Function overloading in C# can be implemented by changing the number of parameters and the data type of the parameters.

Suppose you have a function that prints the multiplication of numbers, then our overloaded methods will have the same name but different number of arguments -

public static int mulDisplay(int one, int two) { }
public static int mulDisplay(int one, int two, int three) { }
public static int mulDisplay(int one, int two, int three, int four) { }
Copy after login

The following example shows how to implement function overloading -

Example

Live demonstration

using System;
public class Demo {
   public static int mulDisplay(int one, int two) {
      return one * two;
   }

   public static int mulDisplay(int one, int two, int three) {
      return one * two * three;
   }
   
   public static int mulDisplay(int one, int two, int three, int four) {
      return one * two * three * four;
   }
}

public class Program {
   public static void Main() {
      Console.WriteLine("Multiplication of two numbers: "+Demo.mulDisplay(10, 15));
      Console.WriteLine("Multiplication of three numbers: "+Demo.mulDisplay(8, 13, 20));
      Console.WriteLine("Multiplication of four numbers: "+Demo.mulDisplay(3, 7, 10, 7));
   }
}
Copy after login

Output

Multiplication of two numbers: 150
Multiplication of three numbers: 2080
Multiplication of four numbers: 1470
Copy after login

Operator overloading

Overloaded operators are functions with special names, key A literal operator is followed by the symbol of the defined operator.

The following shows which operators can be overloaded and which operators can be overloaded but not overloaded-

##456
Sr.No Operation Symbol and description
1 , - , !, ~, , --

These unary operators take one operand and can be overloaded.

2 , -, *, /, %

These binary operators Takes an operand and can be overloaded.

3 ==、!=、、=

Comparison operators can be overloaded.

&&, ||Conditional logical operators cannot be overloaded directly.

=、-=、*=、/=、%= Assignment operators cannot be overloaded.

=, ., ?:, -These operators cannot be overloaded

The above is the detailed content of What is overloading in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template