I try to define an interface like this:
public interface Something
{
void f();
}
public interface SomethingElse
{
void g(Something s);
}
This should say that g takes a parameter that adheres to the interface "Something". When I try to create classes the compiler will reject the SomethingElse class because it does not match the signature of the interface definition. But how should I define the interface?
class Something_class : Something
{
public void f() { Console.WriteLine("Something.f"); }
}
class SomethingElse_class : SomethingElse
{
public void g(Something_class s) { Console.WriteLine("SomethingElse.g"); }
}
Your SomethingElse
interface says that any implementation needs to accept any type which implements Something
- not just one specific example.
Either you need to change SomethingElse_class
to:
class SomethingElse_class : SomethingElse
{
public void g(Something s) { Console.WriteLine("SomethingElse.g"); }
}
... or make SomethingElse
generic, e.g.
public interface SomethingElse<T> where T : Something
{
void g(T s);
}
then SomethingElse_class
would be:
class SomethingElse_class : SomethingElse<Something_class>
{
public void g(Something_class s) { ... }
}
See more on this question at Stackoverflow