Questionn on interfaces


O

Ohad Young

Hi,

I have a question on interfaces in C#:
I have a simple interface that is implemented by a class. However, the class
accessibility modifier is internal, while
all interface members are public by default. What is the accessibility of
the implemented interface member?

Here is the code sample:

interface IMyInterface
{
void SomeMethod();
}

internal class MyClass : IMyInterface
{
public void SomeMethod()
{
//do something
}
}

Regards, Ohad
--
Ohad Young
Medical Informatics Research Center
Ben Gurion University
Information System Eng
Office Phone: 972-8-6477160
Cellular Phone: 972-54-518301
E-Mail: (e-mail address removed)
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Ohad Young said:
I have a question on interfaces in C#:
I have a simple interface that is implemented by a class. However, the class
accessibility modifier is internal, while
all interface members are public by default. What is the accessibility of
the implemented interface member?

All interface members are implicitly public.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Ohad,
That is very good question. And not so easy to answer.

Let's look at the classes first

internal class Bar
{
public void Method()
{
}
}

The method is *public*, but overall visibility of the class is *internal*,
whcih means that you cannot export that class outside the assembly and
cannot access the method respectively. In onther words access to the method
is restricted by the visiblity of the class.

The same goes for intefaces, which members are public by default.
Accessibility of the interface method is restricted by the overall
visibility of the interface.

When it goes to the interfece members implementation thinks get a bit more
complicated

Examine this
First case:
========
internal interface IFoo
{
void Method();
}

public class Bar:IFoo
{
public void Method()
{
}
}

The interface cannot be exported outside the assembly, but interface
member's implemetation can be accessed outside the assembly thru reference
to the class instance. The class is public.

Second case
=========
public interface IFoo
{
void Method();
}

internal class Bar:IFoo
{
public void Method()
{
}
}

The class cannot be exported outside the assembly, but the interface is
public so it can. Thus, Method implementation can be accessed thru the
interface

Third case:
==========
<public or internal> interface IFoo
{
void Method();
}

internal class Bar:IFoo
{
//Explicit implementation
public void IFoo.Method()
{
}
}

The class doesn't export the implementation at all (it is private) and the
access to the Method's implementation is controlled only by the overall
visibility of the interface.

So my answer of your question is:
If the interface member is not explicitly implemented by a class the
visibility of the implementation is the less restrictive one of overall
class visbility and overall interface visibility; If the inteface member is
implemented explicitly by a class then that member implementation has the
overall visibility of the interface.

Got lost? :)
 
G

Guest

Hi Ohad,

Thank you for posting in the community!

Based on my understanding, you want to know if the interface and implement
class have different accessibility, what will be the result accessibility
of the methods.

==========================================
I think the community member Stoitcho has provided you a full description
and example about all the possible issue.(Thanks for his contribution to
the community!)

But I want to add some more information.

First, "Interface members implicitly have public declared accessibility. No
access modifiers are allowed on interface member declarations.", so in
Stoitcho's case 3, the explicit implement of interface should not be
applied any access modifier(Public should be removed). Anyway, this is a
little thing :)

For the test of Stoitcho's sample, in case 2 and 3, because the class is
marked as "internal", so you can not create an instance of the class in
EXTERNAL assembly. I think you should use a "help" class to return a
reference of the class(Actually it is a reference to the interface) for
external assembly.

For case 4(The interface and class are both internal), there is no way to
use them, but you can use Reflection to invoke the member of the class and
interface, I provide a sample in the code. Reflection is powerful tool,
which can even invoke a class's private member. But it needs a high
privilege on that code.(Which is not recommanded to use, this is just a
sample for you.)

Sample code like this:
//The implement assembly code
namespace internalinterface
{
internal interface IFoo1
{
void Method();
}

public class Bar1:IFoo1
{
public void Method()
{
Console.WriteLine("Bar1 output");
}
}

public interface IFoo2
{
void Method();
}

internal class Bar2:IFoo2
{
public void Method()
{
Console.WriteLine("Bar2 output");
}
}

public interface IFoo3
{
void Method();
}

internal class Bar3:IFoo3
{
void IFoo3.Method()
{
Console.WriteLine("Bar3 output");
}
}

internal interface IFoo4
{
void Method();
}

internal class Bar4:IFoo4
{
void IFoo4.Method()
{
Console.WriteLine("Bar4 output");
}
}

public class helpIFoo2
{
public IFoo2 getinterface()
{
Bar2 obj=new Bar2();
return (IFoo2)obj;
}
}

public class helpIFoo3
{
public IFoo3 getinterface()
{
Bar3 obj=new Bar3();
return (IFoo3)obj;
}
}
}

//The test assembly's code
class Class1
{
[STAThread]
static void Main(string[] args)
{
internalinterface.Bar1 obj1=new internalinterface.Bar1();
obj1.Method();

internalinterface.helpIFoo2 h2=new internalinterface.helpIFoo2();
internalinterface.IFoo2 obj2=h2.getinterface();
obj2.Method();

internalinterface.helpIFoo3 h3=new internalinterface.helpIFoo3();
internalinterface.IFoo3 obj3=h3.getinterface();
obj3.Method();

try
{
Assembly ass=Assembly.LoadFrom("internalinterface.dll");
object objclass=ass.CreateInstance("internalinterface.Bar4");
Type interfacetype=ass.GetType("internalinterface.IFoo4");
if(objclass!=null)
{
MethodInfo meth=interfacetype.GetMethod("Method");
meth.Invoke(objclass,new object[]{});
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

==========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jeffrey Tan[MSFT],
I think the community member Stoitcho has provided you a full description
and example about all the possible issue.(Thanks for his contribution to
the community!)

Thanks.. I appreciate this.
But I want to add some more information.

First, "Interface members implicitly have public declared accessibility. No
access modifiers are allowed on interface member declarations.", so in
Stoitcho's case 3, the explicit implement of interface should not be
applied any access modifier(Public should be removed). Anyway, this is a
little thing :)

Yes, thanks for the correction. Should have been more careful with
copy/paste :)
For the test of Stoitcho's sample, in case 2 and 3, because the class is
marked as "internal", so you can not create an instance of the class in
EXTERNAL assembly. I think you should use a "help" class to return a
reference of the class(Actually it is a reference to the interface) for
external assembly.

That is perfectly right. All my examples are for using interface method's
implementation assuming that the class is instantiated and the consumer has
reference to the object or the interface
For case 4(The interface and class are both internal), there is no way to
use them, but you can use Reflection to invoke the member of the class and
interface, I provide a sample in the code. Reflection is powerful tool,
which can even invoke a class's private member. But it needs a high
privilege on that code.(Which is not recommanded to use, this is just a
sample for you.)

Maybe I should have split my third case. I completely agree of what you said
about refelection. Further more I believe that eventhough we have that
powerfull tool Reflection in our hands if somethig is declared as *internal*
that means it is not supposed to be used by anyone, but its creator. It is
not documented and no guesses should be made about its functionality. It
might be changed or even removed from the next versions of the assembly(in
the cases where the class library assembly is privately depoloyed) without
any notice or whatsoever.

And finaly I want to thank you Jeffrey for the complete example coverring
all casses you provide. I believe afterthat everything should be clear.
 
O

Ohad Young

Hi,

Thank you all for the detailed replies.
The examples you gave had made the subject clear.

Till next question, Ohad
 
Ad

Advertisements

G

Guest

Hi Ohad,

Thanks very much for your feedback.

I am glad our reply makes sense to you. If you have any further question,
please feel free to post, we will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top