JohnB
JohnB

Reputation: 13733

Public interface deriving from internal?

An assembly exposes several interfaces (IFirst, ISecond, IThird) to the outside world, i.e. those interfaces are public.

Now, as an implementation detail, all of these objects share a common characteristic, described by interface IBase. I do not want to make IBase public, as this may change in future implementations and is completely irrelevant for the users of my assembly.

Obviously, a public interface cannot derive from an internal one (gives me a compiler error).

Is there any way to express that IFirst, ISecond, and IThird have something in common from an internal point of view only?

Upvotes: 4

Views: 595

Answers (2)

Matthew Watson
Matthew Watson

Reputation: 109792

As Andrew said above. Just to expand, here's a code sample:

public interface IFirst
{
    string FirstMethod();
}

public interface ISecond
{
    string SecondMethod();
}

internal interface IBase
{
    string BaseMethod();
}

public class First: IFirst, IBase
{
    public static IFirst Create()  // Don't really need a factory method;
    {                              // this is just as an example.
        return new First();
    }

    private First()  // Don't really need to make this private,
    {                // I'm just doing this as an example.
    }

    public string FirstMethod()
    {
        return "FirstMethod";
    }

    public string BaseMethod()
    {
        return "BaseMethod";
    }
}

public class Second: ISecond, IBase
{
    public static ISecond Create()  // Don't really need a factory method;
    {                               // this is just as an example.
        return new Second();
    }

    private Second()  // Don't really need to make this private,
    {                 // I'm just doing this as an example.
    }

    public string SecondMethod()
    {
        return "SecondMethod";
    }

    public string BaseMethod()
    {
        return "BaseMethod";
    }
}

Upvotes: 1

Andrew Arnott
Andrew Arnott

Reputation: 81801

Not in C#

The best you can do is have your classes implement both internal and public interfaces.

Upvotes: 4

Related Questions