jstark
jstark

Reputation: 131

Implementing an interface with interface members

What is the proper way to implement an interface that has its own interface members? (am I saying that correctly?) Here's what I mean:

public Interface IFoo
{
    string Forty { get; set; }
    string Two { get; set; }
}

public Interface IBar
{
    // other stuff...

    IFoo Answer { get; set; }
}

public class Foo : IFoo
{
    public string Forty { get; set; }
    public string Two { get; set; }
}

public class Bar : IBar
{
    // other stuff

    public Foo Answer { get; set; } //why doesnt' this work?
}

I've gotten around my problem using explicit interface implementation, but I'm wondering if there is a better way?

Upvotes: 3

Views: 490

Answers (4)

Servy
Servy

Reputation: 203802

You'll need to use generics to be able to do what you want.

public interface IFoo
{
    string Forty { get; set; }
    string Two { get; set; }
}

public interface IBar<T>
    where T : IFoo
{
    // other stuff...

    T Answer { get; set; }
}

public class Foo : IFoo
{
    public string Forty { get; set; }
    public string Two { get; set; }
}

public class Bar : IBar<Foo>
{
    // other stuff

    public Foo Answer { get; set; }
}

This will allow you to provide an interface that says something like, "to implement this interface you must have a property with a public getter/setter of a type that implements IFoo." Without generics you are simply saying that the class has a property with a type of exactly IFoo, rather than anything that implements IFoo.

Upvotes: 4

Lews Therin
Lews Therin

Reputation: 10995

Foo may extend type IFoo but that isn't what the interface exposes. An interface defines a contract and you need to obey the terms of that contract. So the correct way is to use public IFoo Answer{get;set;} like others have said.

Upvotes: 0

Louis Kottmann
Louis Kottmann

Reputation: 16618

IBar has a IFoo field, not a Foo one, do this instead:

public class Bar : IBar
{
    // other stuff

    public IFoo Answer { get; set; }
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

You need to use the exact same type as in the interface:

public class Bar : IBar 
{ 
    public IFoo Answer { get; set; }   
} 

Note: IFoo instead of Foo.

The reason is that the interface defines a contract and the contract says that it must be an IFoo.

Think about it:

You have the classes Foo and Foo2, both implement IFoo. According to the contract, instances of both classes can be assigned. Now, if your code was legal, this would somehow break because your class only accepts Foo. Explicit interface implementation doesn't change that fact in any way.

Upvotes: 6

Related Questions