0

Here at work we mainly used Delphi, but also some C#.
So today a college asked me an interesting question today: How do I perform Intreface mapping in C#? And since I do not know here is the question for you:

First the Delphi Example:

unit UnitInterfaceMapping;

interface

type
  IMyFirstInterface = interface(IInterface)
    procedure DoSomethingInteresting;
    procedure DoSomethingElse;
  end;

  IMyNextInterface = interface(IInterface)
    procedure DoSomethingInteresting;
  end;

  TMyCombinedObject = class(TInterfacedObject, IMyFirstInterface, IMyNextInterface)
  private
    procedure IMyFirstInterface.DoSomethingInteresting = DoSomethingInterestingFirst;
    procedure IMyNextInterface.DoSomethingInteresting = DoSomethingInterestingNext;
  public
    procedure DoSomethingInterestingFirst;
    procedure DoSomethingInterestingNext;
    procedure DoSomethingElse;
  end;

implementation

uses
  VCL.Dialogs;
{ TMyCombinedObject }

procedure TMyCombinedObject.DoSomethingElse;
begin
  ShowMessage('DoSomethingElse');
end;

procedure TMyCombinedObject.DoSomethingInterestingFirst;
begin
  ShowMessage('DoSomethingInterestingFirst');
end;

procedure TMyCombinedObject.DoSomethingInterestingNext;
begin
  ShowMessage('DoSomethingInterestingNext');
end;

end.

Form a Form I call my code:

uses
  UnitInterfaceMapping;

procedure TForm14.Button1Click(Sender: TObject);
var
  MyFirstInterface: IMyFirstInterface;
  MyNextInterface: IMyNextInterface;
begin
  MyFirstInterface := TMyCombinedObject.Create;
  MyFirstInterface.DoSomethingInteresting;
  MyFirstInterface.DoSomethingElse;

  MyNextInterface := TMyCombinedObject.Create;
  MyNextInterface.DoSomethingInteresting;
end;

The result is three dialog boxes, one from each method.

Then I tried to port it to C#:

using System;

namespace InterfaceMapping
{

    public interface IMyFirstInterface
    {
        void DoSomethingInteresting();
        void DoSomethingElse();
    }


    public interface IMyNextInterface
    {
        void DoSomethingInteresting();
    }

    public class CombinedObject : IMyFirstInterface, IMyNextInterface
    {
        public void DoSomethingInteresting()
        {
            throw new NotImplementedException();
        }

        void IMyFirstInterface.DoSomethingElse()
        {
            throw new NotImplementedException();
        }

        void IMyFirstInterface.DoSomethingInteresting()
        {
            throw new NotImplementedException();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            new CombinedObject() <== Problem
        }
    }
}

The main point is when creating a new instance of CombinedObject I only see the DoSomethingInteresting method.

So in short: How do I perform Interface mapping in C#

Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • 1
    http://www.c-sharpcorner.com/UploadFile/rohatash/inherit-multiple-interfaces-with-the-same-method-name-in-C-Sharp/ – Sam Axe Oct 31 '17 at 06:18
  • 1
    `IMyFirstInterface obj = new CombinedObject();` you would get both methods. The problem here is you have explicit implementation of Interfaces. – praty Oct 31 '17 at 06:18
  • 1
    You should have a look at implicit and explicit interface implementations. [This](https://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation) question might help you here. – Streamline Oct 31 '17 at 06:22

1 Answers1

1

In order to access methods from IMyFirstInterface interface, you need to explicitly define a type of an object:

IMyFirstInterface obj = new CombinedObject();
obj.DoSomethingInteresting(); //Method is accessible

Lines above will invoke void IMyFirstInterface.DoSomethingInteresting() method, on the other hand, if you instantiate object of type CombinedObject:

var obj = new CombinedObject();
obj.DoSomethingInteresting();

Implicitly implemented interface's method will be invoked: public void DoSomethingInteresting()

Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31