Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

11 August 2012

Can we have an Interface/Class with same method names and signature with different return type


In the interviews, we will face a question like Can we have an Interface/class with same method names and signature with different return type.

No, we can not have same method name and signature with different type in an Interface/class. we will get compile time error as 'Interface/Class_Name' already defines a member called 'Method_Name' with the same parameter types.

Example:



    interface ICalc
    {
        int Add(int a, int b);
        double Add(int a, int b); //Compile Error as 'ICalc' already defines a member called 'Add' with the same parameter types
    }
    class calc
    {
        int Add(int a, int b)
        {
            return a = b;
        }
        double Add(int a, int b) ////Compile Error as 'calc' already defines a member called 'Add' with the same parameter types
        {
            return Convert.ToDouble(a + b);
        }
    }
 

0 Comments:

Post a Comment