Lets say I have an interface like this:
public interface MyInterface
{
int Property1
{
get;
}
void Method1();
void Method2();
}
Is there any way to force the implementer of the interface to implement parts of it explicitly? Something like:
public interface MyInterface
{
int Property1
{
get;
}
explicit void Method1();
explicit void Method2();
}
Edit: As to why I care about whether the interface is implemented explicitly or not; it's not like it matters as far as functionality goes, but it could be helpful to hide some unnecessary details from people using the code.
I'm trying to mimic multiple inheritance in my system, using this pattern:
public interface IMovable
{
MovableComponent MovableComponent
{
get;
}
}
public struct MovableComponent
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
public int Method1()
{
// Implementation
}
public int Method2()
{
// Implementation
}
}
public static IMovableExtensions
{
public static void Method1(this IMovable movableObject)
{
movableObject.MovableComponent.Method1();
}
public static void Method2(this IMovable movableObject)
{
movableObject.MovableComponent.Method2();
}
}
public class MovableObject : IMovable
{
private readonly MovableComponent movableComponent = new MovableComponent();
public MovableComponent MovableComponent
{
get { return movableComponent; } // Preferably hiddem, all it's methods are available through extension methods.
}
}
class Program
{
static void Main(string[] args)
{
MovableObject movableObject = new MovableObject();
movableObject.Method1(); // Extension method
movableObject.Method2(); // Extension method
movableObject.MovableComponent // Should preferably be hidden.
}
}
If the MovableComponent property was implemented explicitly, it would, in most cases, be hidden from whoever was using the class. Hope that explanation wasn't too horrible.
An explicit interface implementation doesn't have an access modifier since it isn't accessible as a member of the type it's defined in. Instead, it's only accessible when called through an instance of the interface.
With implicit interface implementations, the members of the interface are public in the class. With explicit implementations, in the class the interface members are not declared as public members and cannot be directly accessed using an instance of the class, but a cast to the interface allows accessing the members.
Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces. A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.
Read( ) is then overridden in a Note type that derives from Document . using System; interface IStorable { void Read( ); void Write( ); } // Simplify Document to implement only IStorable public class Document : IStorable { // the document constructor public Document(string s) { Console.
No, it is not possible to force implementers to pick explicit or implicit implementation of an interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With