Click here to Skip to main content
15,886,562 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi everyone, I have a question that how can we implement the interfaces having same methodnames like this:
C#
interface ISample2
{
    string CurrentTime();
    string CurrentTime(string name);
}

interface ISample1
{
    string CurrentTime();
}

I did like this:
class TwoInterfacesHavingSameMethodName : ISample1, ISample2
{
    static void Main(string[] sai)
    {
        ISample1 obj1 = new TwoInterfacesHavingSameMethodName();
        Console.Write(obj1.CurrentTime());
        ISample2 obj2 = new TwoInterfacesHavingSameMethodName();
        Console.Write(obj2.CurrentTime("SAI"));
        Console.ReadKey();
    }

    #region ISample1 Members

    string ISample1.CurrentTime()
    {
        return "Interface1:" + DateTime.Now.ToString();
    }

    #endregion

    #region ISample2 Members

    string ISample2.CurrentTime()
    {
        return "Interface2:FirstMethod" + DateTime.Now.ToString();
    }

    string ISample2.CurrentTime(string name)
    {
        return "Interface2:SecondMethod" + DateTime.Now.ToString() + "" + name;
    }

    #endregion
}

Here what is the meaning of this line:
ISample1 obj1 = new TwoInterfacesHavingSameMethodName();

Are we creating object for Class or Interface. What is the basic use of writing the methods in Interface?
Posted
Updated 26-Oct-10 9:34am
v4
Comments
Yusuf 26-Oct-10 15:38pm    
You can not instantiate Interface, so always you will create object for class.

In general, I'd try not to name methods between two interfaces the same thing. I know sometimes it may not be avoidable, but think about the readability of the code.

Ths basic use is to encapsule a related set of operations behind an interface, this interface can be common to more than one class. The idea is to reduce coupling between the consumer of the interface and implementing class.

In the example you gave you can access both interfaces from the same instance e.g.

TwoInterfacesHavingSameMethodName obj =  new TwoInterfacesHavingSameMethodName();

ISample1 sample1 = (ISample1)obj;
ISample2 sample2 = (ISample2)obj;
 
Share this answer
 
[This article] explains interfaces in C#.

In your example below you are creating an instance of TwoInterfacesHavingSameMethodName, but it is implicitly converted because you're declaring it as ISample1.

ISample1 obj1 = new TwoInterfacesHavingSameMethodName();


An interface is like a contract. By implementing an interface you're making your class abide by the contract defined in the interface. Anything using that object instance doesn't need to know anything about the instance itself, just the interface it implements, ie. the defined contract.
 
Share this answer
 
To add to the other answers, you only need to implement one method and both interfaces will be assumed to be implemented. Example:

C#
interface IOne
{
    void Foo();
}

interface ITwo
{
    void Foo();
}

class Program : IOne, ITwo
{
    static void Main(string[] args)
    {
        IOne one = new Program();
        ITwo two = new Program();
        one.Foo();
        two.Foo();
    }

    public void Foo()
    {
        Console.WriteLine("Foo");
    }
}
 
Share this answer
 
You can not instantiate an interface,however, u can create
its reference variabe.
You can just assign an object of a class (implementing the
interface) to the reference variable of the interface.
For more reference you should move for Google.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900