Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives inline interface implementation in C#

I'd like to use inline interface implementation in C# but reading some posts like this or this I found out that it's not like Java do it.

Supposing this interface:

public interface MyListener {
    void onHandleOne();
    void onHandleTwo();
    }

and I pass this interface as a parameter:

   myMethod(MyListener listener){
    //some logic
   }

and when I call it I'd like to do inline imlementation like in java:

myMethod(new MyListener () {
                    @Override
                    public void onHandleOne() {
                        //do work
                    }

                    @Override
                    public void onHandleTwo() {
                        //do work
                    }
                });

As an alternative I made a class that implements yhis interface and use this class to call my method:

public class MyImplementor : MyListener  {
    public void onHandleOne() {
        //do work
        }

    public void onHandleTwo() {
        //do work
        }
    }

and call my method: myMethod(new MyImplementor()) but this solutions needs a new class every time I'll call this method (for different behavior) maybe is there a way using lambda or somehow to do it like:

myMethod(new MyImplementor() =>{//handle my methods})

like image 544
Vasile Doe Avatar asked Mar 21 '16 17:03

Vasile Doe


People also ask

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.

Can an anonymous class implement interface?

No, anonymous types cannot implement an interface. We need to create your own type. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

Can an interface implement another interface in C#?

C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.

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

but this solutions needs a new class every time I'll call this method (for different behavior) maybe is there a way using lambda or somehow to do it like

Yes, give it a delegate parameter and pass it a lambda.

public class MyImplementor : MyListener  
{
    private readonly Action handle1;
    private readonly Action handle2;
    public MyImplementor(Action handle1, Action handle2)
    {
        this.handle1 = handle1;
        this.handle2 = handle2;
    }

    public void onHandleOne() 
    {
       handle1();
    }

    public void onHandleTwo()
    {
       handle2();
    }
}

Then you can use it as

myMethod(new MyImplementor(()=>{//handle method1}, ()=>{//Handle method2}); 
like image 64
Sriram Sakthivel Avatar answered Oct 03 '22 06:10

Sriram Sakthivel