Monday 15 September 2014

Method override interview question in C#

class myClass
    {
        public void myFun(out int a) // ERROR
        {
            a = 10;
        }
        public void myFun(ref int a)
        {
            a=20;
        }
  }
Error:- Cannot define overloaded method 'myFun' because
it differs from another method only on ref and out. And Ref and Out
used Pass by reference for parameter Passing.


You can't use the ref and out keywords for the following kinds of methods:
  • Async methods, which you define by using the async modifier.
  • Iterator methods, which include a yield return or yield break statement.
-----------------------------------------------------------------------------

class myClass
{
 public void myFun(int a)  //No Error
 {
     a = 10;
  }
 public void myFun(ref int a)
 {
    a=20;
  }
 }
-----------------------------------------------------------------------------
void myFun(object x) {}
void myFun(dynamic x) {}//No Error.



No comments:

Post a Comment