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}) 
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.
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.
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.
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.
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}); 
                        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