L
L
Lof2011-09-22 13:25:41
C++ / C#
Lof, 2011-09-22 13:25:41

Why is there such a strange restriction on the implementation of the internal interface in C#?

internal interface IFoo
{
void foo();
}

public class Foo : IFoo
{
internal void foo()
{
throw new NotImplementedException();
}
}


Program.cs(8,14): error CS0737: 'Foo' does not implement interface member 'IFoo.foo()'. 'Foo.foo()' cannot implement an interface member because it is not public.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dzuba, 2011-09-22
@Dzuba

Because interface, by definition, only contains descriptions of public members.
Use an explicit interface method implementation:

void IFoo.foo()
{
  throw new NotImplementedException();
}

Call:
Foo obj = new Foo();
((IFoo)obj).foo();

A
Anatoly, 2011-09-22
@taliban

"Members of interfaces are automatically public, and they cannot include any access modifiers."
msdn.microsoft.com/en-us/library/ms173156.aspx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question