Tuesday 6 October 2015

Can we overload methods in following way?

Can we overload methods in following way?

public void MySampleMethod(int i) { }
public void MySampleMethod(ref int i) { }
The signature of a method consists of the name of the method,
the number of type parameters and the type and kind (value, reference, or output)
of each of its formal parameters, considered in the order left to right.

So with above code as there is use of "ref" keyword which makes it unique signature
comapre to other one i.e. we can overload method in above way.

Can we overload methods in following way?

public void MyTestMethod(ref int x){}
public void MyTestMethod(out int x){}

though kind of parameter is a part of signature as
like previous case but here you have used ref and out toghether which is providing
same meaning to the methods therefor you cannot overload methods in same class or
interface solely by ref and out so above code will throw following error.

"Cannot define overloaded method 'MyTestMethod' because it differs from another method only on ref and out"

No comments:

Post a Comment