Implementation of interface in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

HI!
Is it possible to implement the methods of an interface as private.
For exemple, I have an interface :
public interface ITest{
void MyMethod();
}

I want to create a class like this:
public class MyTest : ITest{
private void MyMethod(){
}
}

where the private method MyMethod is the implementation of the ITest's method.
Using VB.Net this is possible. But I don't know the way to do it with C#.
Thanks for any response.
 
HI!
Is it possible to implement the methods of an interface as private.
For exemple, I have an interface :
public interface ITest{
void MyMethod();
}

I want to create a class like this:
public class MyTest : ITest{
private void MyMethod(){
}
}

where the private method MyMethod is the implementation of the ITest's method.
Using VB.Net this is possible. But I don't know the way to do it with C#.
Thanks for any response.

No, of course that is not possible. And by the definition of what an
Interface is, it really does not make sense! I don't see the case why this
is possible in VB.NET, it doesn't make sense to me. But I may miss
something here that I don't see at the moment.

Why do you want to do that?
 
This is possible in VB.Net. For exemple, you can write:

Public Interface ITest
Sub MyMethod()
End Interface

Public Class TestInterface
Implements ITest

Private Sub MyMethod() Implements ITest.MyMethod

End Sub
End Class

Compilers doesn't say anything. In this case users of your class have to
declare any variable as ITest to use methods of TestInterface. They have no
other way to call MyMethod() method.
 
This is possible in VB.Net. For exemple, you can write:

Public Interface ITest
Sub MyMethod()
End Interface

Public Class TestInterface
Implements ITest

Private Sub MyMethod() Implements ITest.MyMethod

End Sub
End Class

Compilers doesn't say anything. In this case users of your class have to
declare any variable as ITest to use methods of TestInterface. They have no
other way to call MyMethod() method.
 
There are two options available for interface implementation, implicit and
explicit. With implicit implementation, the class directly exposes the interface
member as one of it's own members. With explicit implementation the members
can only be accessed via an interface reference, and not from a class reference.

Both methods are possible in VB.NET and C#. Using your original example the
following will perform explicit implementation.
public class MyTest : ITest{
void ITest.MyMethod(){
}
}

Please note that this is not strictly speaking private, since the member
can still be accessed as you noted via casting the class reference to the
interface reference.
 
As Claudio said, the point of an interface is to ensure that any class that
interfaces it will implement and publicly expose every method in the
interface so that any external code invoking members on your object (that
interfaces the interface) can be assured that it's not calling a null
reference.

It seems that what you might be better off doing is instead of using an
interface, is to write a base class and derive your related classes from that
base. This way your code can expect all the public members to be implemented
(either in the base class itself or overriden in the derrived class) and your
"private" members in the base class should be defied as "protected" to only
allow the derived class to call/override them, and not make them visible when
your derived object is down-casted to its base class.
 
Back
Top