Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No-op lambda

Tags:

I have an event on one of my classes that I want to attach a handler to. However, I don't need the handler to do anything, as I am just testing the behaviour of the class with handlers attached or not.

The event signature is as follows:

public event EventHandler<EventArgs> Foo; 

So I want to do something like:

myClass.Foo += (); 

However this isn't a valid lambda expression. What is the most succinct way to express this?

like image 854
Matt Howells Avatar asked Jul 08 '09 13:07

Matt Howells


People also ask

What is a no op Java?

A no op (or no-op), for no operation , is a computer instruction that takes up a small amount of space but specifies no operation. The computer processor simply moves to the next sequential instruction.

Can lambda expression have empty body?

You can think of lambda expressions as anonymous methods (or functions) as they don't have a name. A lambda expression can have zero (represented by empty parentheses), one or more parameters. The type of the parameters can be declared explicitly, or it can be inferred from the context.

Which can be used instead of lambda expression?

How to replace lambda expression with method reference in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference.

Is lambda expression necessary?

Java For Testers Enables functional programming: All new JVM based languages take advantage of the functional paradigm in their applications, but programmers forced to work with Object-Oriented Programming (OOPS) till lambda expressions came. Hence lambda expressions enable us to write functional code.


1 Answers

myClass.Foo += (s,e) => {}; 

or

myClass.Foo += delegate {}; 
like image 169
Matt Howells Avatar answered Oct 02 '22 09:10

Matt Howells