Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to force explicit implementation of an interface (or parts of it)?

Tags:

c#

.net

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.

like image 581
H.S. Avatar asked Dec 12 '12 01:12

H.S.


People also ask

How do you implement an explicit interface?

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.

What is implicit and explicit implementation of 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.

How do you use an interface without implementation?

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.

How do you override an interface in C#?

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.


1 Answers

No, it is not possible to force implementers to pick explicit or implicit implementation of an interface.

like image 179
Alexei Levenkov Avatar answered Sep 29 '22 10:09

Alexei Levenkov