Aizen Aizen - 7 months ago 17
Java Question

C# vs JAVA instance of Interface

I am not sure how am I suppose to go about my question. It is about Android can Instantiate Interface. I am trying to do in C#. Now I am pretty sure that the rules for both Java and C# is you can't create an Instance of abstract and Interface as being said.

But I would really like to know how Android does this practice.

In Android you can do this.

public interface Checkme{
void Test();
void Test2();
}


public void myFunc(Checkme my){
//do something
}

// Now this is the actual usage.

public void Start(){

myFunc(new Checkme(){
@Override
public void Test()
{


}
@Override
public void Test2()
{


}
});

}


Actually once you press Enter on
new Checkme()
You will automatically get the Override methods of the Interface. Like auto Implement method of an Interface in C#.

I hope my question make sense.

Answer

C# doesn't support anonymously auto-implemented interfaces because it has delegates:

public void Foo(Func<string> func) {}

// call it somewhere:
instance.Foo(() => "hello world");

With delegates you can fill the gap and it can be even more powerful than implementing interfaces with anonymous classes.

Learn more about delegates.

Comments