Liam
Liam

Reputation: 29750

Implementing internal class with internal interface error

Why can't I do this?

internal class InsuranceClientFactory : IInsuranceClientFactory
{
    internal Iws2SoapClient InsuranceClient()
    {

    }
}

internal interface IInsuranceClientFactory
{
  Iws2SoapClient InsuranceClient();
}

I get the error:

Cannot implement an interface member because it is not public

There are a few questions where the class is public but the interface is not, but both of mine are internal. I don't want to expose this class or interface outside of my assembly. Seems an odd limitation.

This question C# internal interface with internal implementation, does state "If you are implicitly implementing an interface I believe that the member must be declared public." but this doesn't make sense to me, why can't you? seems justified?

Upvotes: 3

Views: 643

Answers (4)

randcd
randcd

Reputation: 2293

I can tell you why it doesn't work. C# Language specification 5.0 section 13.4.4, Interface Mapping.

... Neither non-public nor static members participate in interface mapping ...

but because what you are attempting to do should technically work, the explicit implementation is given precedence and your implementation can remain internal

And not to split hairs, but even if you Explicitly Implement the internal interface, the implementation will not be internal it is actually private/public as stated earlier in the specification (Section 13.4.1 Explicit Interface Member Implementation):

Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.

Upvotes: 1

Binary Worrier
Binary Worrier

Reputation: 51719

"I don't want to expose this class or interface . . ." and that fine.

But the method needs to be public, even if it is public it won't be visible outside the assembly because the class is internal.

If you really REALLY don't want it to be public, you can explicitly implement that method.

internal class InsuranceClientFactory : IInsuranceClientFactory
{
    Iws2SoapClient IInsuranceClientFactory.InsuranceClient()
    {

    }
}

Upvotes: 3

LarsTech
LarsTech

Reputation: 81665

Change it to:

internal class InsuranceClientFactory : IInsuranceClientFactory
{
    public Iws2SoapClient InsuranceClient()
    {

    }
}

The class is still internal and isn't exposing the interface.

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

You have to change the method modifier to public

internal class InsuranceClientFactory : IInsuranceClientFactory
{
    public Iws2SoapClient InsuranceClient()
    {

    }
}

Making the method public doesn't mean it can be accessed publicly since the DeclaringType itself internal method will have internal access only.

Upvotes: 2

Related Questions