Why can't an interface method be internal?  
Author Message
perrs





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

When I try to make a method in an interface internal I get a compile error.  Why is this

 

Thanks in advance,

Per Rasmussen.



Visual C#18  
 
 
cverdon





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

Hi,

Methods in an interface must all have the same access. It's the interface that is internal or public, not the methods it contain.

Charles


 
 
perrs





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

I realize that I have the possibility to make the entire interface internal. But that does not change the fact that I cannot make the method internal. Consider this example:

internal interface ImyInterface

{

void f1();

}

class MyClass : ImyInterface

{

public void f1();

}

In this example it is impossible for me to declare the function f1 internal (because this will result in my class not implementing the interface). Is it in any way possible for me to make the function internal If not, why not


 
 
cverdon





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

By definition implementing an interface means that all the functions of the interface are available to everyone.

The onlything that you can do to have f1 internal is to explicitly implement the interface:

class MyClass : ImyInterface

{

#region ImyInterface Members

void ImyInterface.f1()

{

throw new NotImplementedException();

}

#endregion

internal void f1()

{

throw new NotImplementedException();

}

}

But this does not mean that ImyInterface.f1 is not accessible to users, but you can have a different implementation.

MyClass c = new MyClass();

c.f1(); //call to internal method

(c as ImyInterface).f1(); //interface method

Charles


 
 
Peter Ritchie





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

A interface is an outward presentation of a particular implementation. If you want to mandate a particular implementation abstract classes are a means of doing this.

An internal, private, or protected class can still implement an interface and while the implementation of those interface members must be public, use of the class would be restricted to internal, private or protected usage (where private and protected apply only to nested classes).

 
 
perrs





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

Charles: I see what you mean but then there isn't really much good in using an interface.

Peter: One of the main points in an interface is that you can make a class implement several. Since C# doesn't have multiple inheritens I can't do the same with classes.
About making the entire class internal. I have thought about this but my problem is that I would like some of the functions to be public and some to be internal (and some to be private and protected) and some of the functions I want internal should implement an interface.

I can see what you mean concerning that an interface is an outward presentation, but isn't kind of strange that you are prohibited from using interfaces for you own use inside eg. your dll without functions start getting exposed that shouldn't have been exposed This seems like a rather wierd constraint in an otherwise great language (IMO).


 
 
PhilipRieck





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

It just wouldn't make much sense to have a public interface with an internal method in it. Try the game "what if it were possible":

Assume I have an assembly "Asm1" that defines a public ISomeInterface, and it has an internal method "IfMethod".
Now, I create a class in assembly "Asm1" that implements it, and a class in assembly "Asm2" that implements it.

Think of this code:

ISomeInterface a = GetImplementation(); // uses a factory method to create a class that implements ISomeInterface
a.IfMethod(); //Call the method

It doesn't matter what assembly this is in - we have no idea what assembly the actual class defining the object "a" is in. So is the call to the "internal" interface method valid There's no way to know at compile time at all.

So really, the only way that makes sense is that if you can access the interface, you can access it's methods.


 
 
perrs





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

I am not talking about a public interface. I am talking about a public class with some public and some internal methods. That class then implements an internal interface with a method declaration which (apparently) has to be public.
 
 
cverdon





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

What you could do is have your class implement 2 interfaces.

One of the interface is declared internal and implemented explicitly by the class. The other interface is public and implemented normally.

Assemblies other than your's will not have access to the internal methods because they will be unable to cast to that interface because it's internal. They will only have access to the public interface members.

Charles


 
 
PhilipRieck





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

 

Perrs : I understand  now, sorry I had your question wrong.

 Yes, you are running into a "limitation" of the c# language (or possibly the CLR - but without investigation I suspect it's c#).  

Can't tell you a "fix", since it's just the way it is.  The best work around I can suggest is exactly what cverdon suggested.




internal interface ISomething
{
  void Func();
}
public class Aclass : ISomething
{
  void ISomething.Func() { this.Func(); }

  internal void Func() { ...}
}

 

 

This way, the method "Func" is internal, and "ISomething.Func" (while still public) can only be called through a ISomething object - which is internal.


 
 
perrs





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

Charles: What do you mean by "implemented explicitly"

Philip: AFAIK this approach will not prevent an outside assembly to access Func() which was my purpose.

Edit: What I mean is: everybody can call Aclass.Func() as this still have to be public because of the interface. See what I mean


 
 
PhilipRieck





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

No, they can not call Aclass.Func(), because it is internal. ((ISomething)Aclass).Func() is public, but you can't cast to ISomething outside the assembly because it is internal.

The only way to call into ((ISomething)Aclass).Func() would be with reflection.


 
 
cverdon





PostPosted: Visual C# Language, Why can't an interface method be internal? Top

An interface is implemented explicitly with this syntax:

void ISomething.Func() { this.Func(); }

Philip and me are proposing the same "solution"...

Charles