-2

Is there a single line quivalent for this:

void AcceptChanges() => Data != null ? DoSomethingWithData(Data);

I wonder why this has not been addressed in c# so far.

because of the void return type its also not possible to do something like this

void AcceptChanges() => Data != null ? DoSomethingWithData(Data) : null;

Would be nicer to do something like this to intend No-call

void AcceptChanges() => Data != null ? DoSomethingWithData(Data) : void;

or new meaning of ?? (as the bool-expression iitself cannot be null in this case)

void AcceptChanges() => Data != null ?? DoSomethingWithData(Data);

Edit: The closest solution for this is using a Delegate:

using System;
using System.Diagnostics.CodeAnalysis;

public static class VoidHelper
{
    // ReSharper disable once UnusedParameter.Global
    public static void RunVoid([NotNull] this object source, bool condition, Action action) =>
        RunVoid(condition, action);
    public static void RunVoid(bool condition, Action action)
    {
        if (condition)
            action();
    }

    // ReSharper disable once UnusedParameter.Global
    public static void RunVoid<T>([NotNull] this object source, bool condition, Action<T> action, T arg) =>
        RunVoid(condition, action, arg);
    public static void RunVoid<T>(bool condition, Action<T> action, T arg)
    {
        if (condition)
            action(arg);
    }

    // Add more args if neccessary

    public static void Test(object data) => RunVoid(data != null, () => DoSomethingWithData(data));

    private static void DoSomethingWithData(object data) { }
}
CleanCoder
  • 2,406
  • 1
  • 10
  • 35
  • 3
    Why not just `if(Data != null) DoSomethingWithData(Data);`? – Jonathon Chase Mar 06 '20 at 16:29
  • @JonathonChase That can't be used as an expression-bodied function. –  Mar 06 '20 at 16:31
  • `I wonder why this has not been addressed in c# so far.` ← That is not a question. If you want a feature request then [so] is also the wrong place to ask. As to why it is not implemented as a language spec that is speculation and will only draw answers rooted in opinion. – Igor Mar 06 '20 at 16:32
  • 2
    Suggested reading by Eric Lippert: https://stackoverflow.com/a/2806990/47589 –  Mar 06 '20 at 16:34

1 Answers1

1

Option 1: Convert your method to an extension method and use the null-propagating operator. The method only gets called if the instance is not null.

void AcceptChanges() => Data?.DoSomethingWithData();

Option 2: Don't use expression bodied statements and use a simple null check in an if.

void AcceptChanges()
{
    if (Data != null)
    {
        DoSomethingWithData(Data);
    }
}
Chronicle
  • 1,565
  • 3
  • 22
  • 28
  • 1
    @sven-krauter I believe option 1 here is what you are looking for – David Watts Mar 06 '20 at 16:35
  • Option 1 is logically wrong. the object containing AcceptChanges should do something with data, not the extension. – CleanCoder Mar 06 '20 at 16:38
  • 3
    @SvenKrauter Extension methods compile from `SomeObject.SomeExtensionMethod();` to `SomeStaticClass.SomeExtensionMethod(SomeObject);`, which is what Option 1 is suggesting. – Jonathon Chase Mar 06 '20 at 16:39