Search code examples
c#interfaceexplicit-interface

What is this C# interface syntax called and what does it mean


I am working on a rather complex set of interfaces which allows to define objects with a specific structure. Via interfaces and generics it also allows me to define a composition of interfaces which are available in each element. The structure maps something in our background and so the goal was to recreate the structure in the code for better understanding what is actually manipulated with the code.

The result looks like this:

static void Main(string[] args)
{

  IComplexDefinition1 CD1 = null;
  
  CD1.Access.Level1.ElementB.SomeStructrueMethod();
  CD1.Access.Level1.ElementC.ActorAMethod1();
  CD1.Access.Level1.ElementC.ActorBMethod2();
  CD1.Access.Level1.ElementC.ActorCMethod1();

  CD1.Access.Level2.ElementA.SomeOperationPoolMethod();
  CD1.Access.Level2.ElementC.ActorAMethod2();
  CD1.Access.Level2.ElementC.ActorBMethod1();
  CD1.Access.Level2.ElementC.ActorCMethod2();

}

Now on implementing the concrete class I clicked "auto implement" in Visual Studio and it came up with a line of code I haven't seen so far (and didn't expect that is occurs). So I am looking for insight how it works and where I can read/study more about it.

The relevant parts of the concrete implementation are:

public class ComplexDefinition : IComplexDefinition1,....
{

  protected IActorComposition _actors;
  protected SomeStructure _structure;
  protected SomeOperationPool _operationPool;

  public IElementsBC<IActorCompositionLevel2, SomeStructure> Level1 => this;
  public IElementsAC<IActorCompositionLevel1, SomeOperationPool> Level2 => this;
  
  public SomeStructure ElementB => _structure;
  public SomeOperationPool ElementA => _operationPool;

  public IActorCompositionLevel2 ElementC => _actors;
  IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC => _actors;

}

And especially those two lines are troubling me (especially the last line):

public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC => _actors;

The first line is clear and as we know it

  [visibility] [Type]                  [Name]   => [what gets returned]
  public       IActorCompositionLevel2 ElementC => _actors;

The second line does not have the same visibility (though both are public) and the type definition is more complex and has an additional dot befor the name (and is the part I haven't seen so far).

IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC

Solution

  • On the last line I think what you're looking for is the Explicit Interface Implementation. If you are implementing 2 different interfaces with the same method then you can differentiate between them using this construct.

    For example:

    public class TestClass : IInterface1, IInterface2
    {
        void IInterface1.SameMethod()
        {
            System.Console.WriteLine("IInterface1");
        }
        void IInterface2.SameMethod()
        {
            System.Console.WriteLine("IInterface2");
        }
    }
    
    // now if you create the class itself then the type you are assigning it into will decide what method it should call
    TestClass test = new TestClass();
    test.SameMethod(); // this won't compile, since it won't know what to call
    
    (test as IInterface1).SameMethod(); // this should display "IInterface1" in console
    

    More on that in official docs.

    The => is the Expression-bodied members, and it basically works like a shorthand for a function that will be called every time the property is accessed. You can think of it as a shorthand for

    IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC {
        get {
            return _actors;
        }
    }
    

    Written by hand, so don't try to compile that. But it should illustrate the point