141

I'd like to declare an "empty" lambda expression that does, well, nothing. Is there a way to do something like this without needing the DoNothing() method?

public MyViewModel()
{
    SomeMenuCommand = new RelayCommand(
            x => DoNothing(),
            x => CanSomeMenuCommandExecute());
}

private void DoNothing()
{
}

private bool CanSomeMenuCommandExecute()
{
    // this depends on my mood
}

My intent in doing this is only control the enabled/disabled state of my WPF command, but that's an aside. Maybe it's just too early in the morning for me, but I imagine there must be a way to just declare the x => DoNothing() lambda expression in some way like this to accomplish the same thing:

SomeMenuCommand = new RelayCommand(
    x => (),
    x => CanSomeMenuCommandExecute());

Is there some way to do this? It just seems unnecessary to need a do-nothing method.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rob
  • 25,984
  • 32
  • 109
  • 155

7 Answers7

282
Action doNothing = () => { };
Rauhotz
  • 7,914
  • 6
  • 40
  • 44
  • 2
    Does there exist a predefined empty lambda? I think that it is a bad idea performance wise to create an empty lambda every time I need it. E.g. in JQuery [there is the `noop`](https://stackoverflow.com/questions/2069345/what-real-purpose-does-noop-serve-in-jquery-1-4) and I would expect something similar to be present in C#. – qqqqqqq Mar 12 '20 at 21:28
  • So is an async version of this require the verbose `Func doNothing = async() => await Task.CompletedTask;`? – Patrick Szalapski Apr 08 '20 at 20:21
27

I thought I would add some code that I've found useful for this type of situation. I have an Actions static class and a Functions static class with some basic functions in them:

public static class Actions
{
  public static void Empty() { }
  public static void Empty<T>(T value) { }
  public static void Empty<T1, T2>(T1 value1, T2 value2) { }
  /* Put as many overloads as you want */
}

public static class Functions
{
  public static T Identity<T>(T value) { return value; }

  public static T0 Default<T0>() { return default(T0); }
  public static T0 Default<T1, T0>(T1 value1) { return default(T0); }
  /* Put as many overloads as you want */

  /* Some other potential methods */
  public static bool IsNull<T>(T entity) where T : class { return entity == null; }
  public static bool IsNonNull<T>(T entity) where T : class { return entity != null; }

  /* Put as many overloads for True and False as you want */
  public static bool True<T>(T entity) { return true; }
  public static bool False<T>(T entity) { return false; }
}

I believe this helps improve readability just a tiny bit:

SomeMenuCommand = new RelayCommand(
        Actions.Empty,
        x => CanSomeMenuCommandExecute());

// Another example:
var lOrderedStrings = GetCollectionOfStrings().OrderBy(Functions.Identity);
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Anthony
  • 9,451
  • 9
  • 45
  • 72
10

This should work:

SomeMenuCommand = new RelayCommand(
    x => {},
    x => CanSomeMenuCommandExecute());
Joseph
  • 25,330
  • 8
  • 76
  • 125
7

Assuming you only need a delegate (rather than an expression tree) then this should work:

SomeMenuCommand = new RelayCommand(
        x => {},
        x => CanSomeMenuCommandExecute());

(That won't work with expression trees as it's got a statement body. See section 4.6 of the C# 3.0 spec for more details.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

I don't fully understand why do you need a DoNothing method.

Can't you just do:

SomeMenuCommand = new RelayCommand(
                null,
                x => CanSomeMenuCommandExecute());
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • 4
    That is probably checked and will probably throw a NRE. – Dykam Nov 16 '09 at 15:50
  • I think Dykam is right, but I just didn't think about passing null :-) – Rob Nov 16 '09 at 17:53
  • 2
    I don't understand why this is downvoted? Jorge makes a valid point, although it would have been a small effort to check it. – Cohen Oct 19 '11 at 12:57
  • 1
    +1, this is a valid solution, just that the null checking should be extended in `new RelayCommand(..`. – nawfal Dec 11 '13 at 14:42
  • Imagine what you would need to do in the method using that "null" Action....checking if (action == null) action(); every time. – Tom Apr 25 '23 at 08:11
1

Starting with C# 9.0 you can specify discards _ for required parameters. Example:

Action<int, string, DateTime> action = (_, _, _) => { };
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0
Action DoNothing = delegate { };
Action DoNothing2 = () => {};

I used to initialize Events to a do nothing action so it was not null and if it was called without subscription it would default to the 'do nothing function' instead of a null-pointer exception.

public event EventHandler<MyHandlerInfo> MyHandlerInfo = delegate { };
Javi Kroonenburg
  • 1,050
  • 8
  • 11