Different ways a method can be overloaded in C#.NET

Options
- Different parameter data types
- Different number of parameters
- Different order of parameters
- a and b


CORRECT ANSWER : a and b

Discussion Board
Wrong option given as right answer

As methods can be overloaded and can be differentiated by their function signatures which are
1.Number of parameters
2.Data types of parameters
3.Order of parameters
So, how can be the answer is a and b?

It would be better if you have a look on it again.

Thank you..

Sowmya 09-20-2020 10:20 AM

WRONG

c) is also correct... SOME of the time, but SOME of the time is enough because the question says "Different ways a method *can be* overloaded in C#.NET".

I CAN overload a method by a different order of parameters. Check this out:

public void Method(int a, string b) { }
public void Method(string b, int a) { }

This IS acceptable, therefore the question is wrong.

Matthew 04-26-2016 02:55 PM

CSharp.NET

Correct Answer : All of the above.

Explanation:

Method Overloading:

- Method overloding allows a class to have two or more methods or functions having same name, if there parameter lists are different.

- The parameter lists can be classified as follows:

1. Different parameter data types
2. Different number of parameters
3. Different order of parameters

- Method overloading is alosi knows as polymorphism.

1. Different parameter data types:

Example:

class MOverloading
{
public void show(int a)
{
//Statements
}
public void show(float a)
{
//Statements
}
}

In the above example show() method is overloaded based on the data type of parameters.


2. Different number of parameters:

When methods or functions name are same but number of parameters or arguments are different.

Example:

Class MOverloading
{
public void show(int a)
{
//Statements
}
public void show(int a, float b)
{
//Statements
}
}

In the above example show() has been overloaded based on the number of arguments.

3. Different order of parameters:

In this, the overloading is based on the order of the data type of parameters.

Example:

Class MOverloading
{
public void show(int a, float b)
{
//Statements
}
public void show(float b, int a)
{
//Statements
}
}

In the above example, both the methods have different sequence or order of data type in parameter list. First method is having parameter list as (int, float) and second is having (float, int). So the order is different, the method can be overloaded without any issues.


Prajakta Pandit 03-11-2016 07:33 AM

Answer Explaiin

Toke me a while to understand. A and B is correct because
A(Different parameter data types) means
void test(int i)
and
void test(float i)

B(Different number of parameters) means
test(int i)
and
void test(int i,int j)


C is wrong because you can't do
MyMethod(int a, int b, int c)
and
MyMethod(int c, int b, int a)


kitman 03-3-2016 09:31 AM

What?

How the ans B become wrong? we can have like

MyMethod(int a, int b)
and
MyMethod(int a, int b, int c)


Ragavendhran N 01-25-2016 03:41 AM

Really?

Dubious to say 'Different Order of parameters' is correct - that would only be true IF the parameters were different data types. For example, you can't have two methods
MyMethod(int a, int b, int c)
and
MyMethod(int c, int b, int a)
These are perceived by the compiler as having the same signature.

Dave 04-18-2015 06:42 AM

.net

Very Nice In This Artcle

sivaprasad 02-17-2015 07:53 AM

hello

very nice

meena 02-10-2015 11:44 PM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement