interface members as internal
-
Is it possible to implement methods from an interface class as
internal
in C#? From my first try, it doesn't look like it is possible (the project won't build):public interface IMyInterface{ string Name{ get; set; } } public class MyClass : IMyInterface{ internal string Name{ get{} set{} } }
...this results in a build error. But this IS possible to do in VB.Net:Public Interface IMyInterface Property Name() as String End Interface Public Class MyClass Implements IMyInterface Friend Property Name() as String Implements IMyInterface.Name Get End Get Set(value as String) End Set End Property End Class
...this generates no errors. Why is this? If VB.Net can do it, the CLR must allow it, so why can't C# do it? Would it be because an internal class member defined in an interface doesn't make much sense from a design standpoint? In other words, an internal interface member kind of defeats the purpose of creating an interface in the first place. Is this just a well-thought-out constraint of C#? - Mike ------------------------- "No human being would stack books like that." - Dr. Venkman -
Is it possible to implement methods from an interface class as
internal
in C#? From my first try, it doesn't look like it is possible (the project won't build):public interface IMyInterface{ string Name{ get; set; } } public class MyClass : IMyInterface{ internal string Name{ get{} set{} } }
...this results in a build error. But this IS possible to do in VB.Net:Public Interface IMyInterface Property Name() as String End Interface Public Class MyClass Implements IMyInterface Friend Property Name() as String Implements IMyInterface.Name Get End Get Set(value as String) End Set End Property End Class
...this generates no errors. Why is this? If VB.Net can do it, the CLR must allow it, so why can't C# do it? Would it be because an internal class member defined in an interface doesn't make much sense from a design standpoint? In other words, an internal interface member kind of defeats the purpose of creating an interface in the first place. Is this just a well-thought-out constraint of C#? - Mike ------------------------- "No human being would stack books like that." - Dr. VenkmanC# can, but you're mixing implementation types. You do this in C# using explicit interface implementations, which is what youre VB.NET snippet is doing:
public class MyClass : IMyInterface
{
void IMyInterface.MyMethod()
{
}
}Notice that there's no access modifier. This would be private. This fairly common throughout the .NET FCL. Think about ADO.NET: there's several interface methods but they're all typed; the explicit interface implementation still works and is used to essentially "hide" the "generic" method so that a typed method can be used.
Microsoft MVP, Visual C# My Articles
-
C# can, but you're mixing implementation types. You do this in C# using explicit interface implementations, which is what youre VB.NET snippet is doing:
public class MyClass : IMyInterface
{
void IMyInterface.MyMethod()
{
}
}Notice that there's no access modifier. This would be private. This fairly common throughout the .NET FCL. Think about ADO.NET: there's several interface methods but they're all typed; the explicit interface implementation still works and is used to essentially "hide" the "generic" method so that a typed method can be used.
Microsoft MVP, Visual C# My Articles
Makes perfect sense. This actually helps open up some other doors that explain a few other things. Muchas gracias. - Mike ------------------------- "No human being would stack books like that." - Dr. Venkman