Implementation of interface in C#

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.
 
C

Claudio Grazioli

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?
 
G

Guest

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.
 
G

Guest

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.
 
S

Sean Hederman

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.
 
G

Guest

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.
 

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