• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

Question - What are the different ways in which a method can be Overloaded in C#?

Answer -

Overloading means when a method has the same name but carries different values to use in a different context. Only the main() method cannot be overloaded.

In order to overload methods in C#, 

Change the number of parameters in a method, or
Change the order of parameters in a method, or
Use different data types for parameters
In these ways, you can overload a method multiple times.
For ex.

public class Area {
   public double area(double x) {
       double area = x * x;
       return area;
   }
   public double area(double a, double b) {
       double area = a * b;
       return area;
   }
}
Here, the method Area is used twice. In the first declaration, one argument is used while in the second one, there were two arguments are used. Using different parameters in the same method, we were able to overload the method area().

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners