23

So I currently have the following code:

    BCLThread bclThread = new BCLThread(() => Thread.Sleep(0));

because I can't think of another way to state that I actually don't want that method to do a thing. Is there any other more elegant way of achieving this?

Thanks

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • 6
    Why would you create a thread that does nothing? – fejesjoco Jan 03 '11 at 08:20
  • 6
    @fejesjoco: to do nothing - but in parallel, so not to disturb the other threads doing nothing :-) – marc_s Jan 03 '11 at 08:23
  • 1
    An even more elegant way to write code that does nothing is not to write any code. Can you give us any more context on why you'd actually want to do this? – Cody Gray - on strike Jan 03 '11 at 08:29
  • 4
    Did I actually ask something that needed context to be answered on? – devoured elysium Jan 03 '11 at 08:29
  • 1
    It depends: Do you like the answer I provided? If not, yes, context would probably have helped. It's difficult to imagine a question concerning "elegance" where context isn't useful. – Cody Gray - on strike Jan 03 '11 at 08:45
  • So I currently have the following code: BCLThread bclThread = new BCLThread(() => Thread.Sleep(0)); because I can't think of another way to state that I actually don't want that method to do a thing. Is there any other more elegant way of achieving this? Thanks – devoured elysium Jan 03 '11 at 08:50
  • 6
    @fejesjoco: I know that this is an old thread, but I'll comment for clarity on why you would do this. This is an implementation of the Null Object Pattern. Rather than check to see if the Action is null every place that it is called, you set the default behavior that you would like if the Action isn't explicitly set. Having a "do nothing" Action as the default allows you to call the Action without checking for null and without consequences if the Action isn't explicitly set. – jmblackmer Sep 17 '14 at 19:53
  • 2
    I second @jmblackmer, there are use cases where it its extremely useful to create an action that does nothing. One such case is when replacing all condition checks with an action that is defined beforehand. I ran performance tests and it turns out to be a lot faster with large number iterations. – Matt Feb 02 '16 at 08:53
  • Still valid today: I have an object which allows me to perform an action on all the child objects it instantiates. I do not want any action to be performed, but still need to provide a delegate (which does nothing). – Ama Jan 20 '21 at 14:50
  • @devouredelysium coming a long time later, but I thought it was worth mentioning that `thread.sleep(0`) has a [behavior](https://stackoverflow.com/a/3257751/10794555) distinct from `thread.sleep(t)` where t>0. – Ama Jan 20 '21 at 14:59

3 Answers3

34

Why not

BCLThread bclThread = new BCLThread(() => {});

?

Victor Haydin
  • 3,518
  • 2
  • 26
  • 41
18
Action a = delegate { };
Action b = () => { };
fejesjoco
  • 11,763
  • 3
  • 35
  • 65
0

Just to add an alternative to () => {} (which I would consider close to canonical).

If you have a code style enforcer / auto-formatter that doesn't like allowing {} on the same line, I've also seen assigning 0 to a discard.

Action a = () => _=0;

Which might be cleaner than

Action a = () => 
{
};

So

new BCLThread(() => _=0);

I would say this also communicates "do nothing", if in a very slightly less elegant way. Both examples produce identical IL code.

(source)

Jay
  • 2,553
  • 3
  • 17
  • 37