Leo Nguyen
Leo Nguyen

Reputation: 614

C# can implement without class declare as Java

Java can implement interface OnCustomEventListener such as:

classA.setCustomEventListener(new OnCustomEventListener(){
public void onEvent(){
   //do whatever you want to do when the event is performed.
}

Does C# can do the same?

Upvotes: 0

Views: 64

Answers (1)

Jamiec
Jamiec

Reputation: 136154

No, C# has no direct equivalent to that code, howeever the typical way you would pass some "action" or "callback" to a method would be using a delegate, or the shorter-formed Lambda expression.

classA.SetCustomEventListener( () => {
   //do whatever you want to do when the event is performed.
});

The code for that method would look like

public void SetCustomEventListener (Action action)
{
    action(); // execute the Lambda passed in
}

Ref:

Upvotes: 1

Related Questions