C# null-conditional shorthand for method arguments
Asked Answered
G

3

5

The null-conditional operator is very useful when the method belongs to the object in question, but what if the object in question is an argument? For example, can this be shortened?

var someList = new List<SomeType>();
if (anotherList.Find(somePredicate) != null)
{
    someList.Add(anotherList.Find(somePredicate))
}

One solution I thought of was to use an extension method like below:

public static void AddTo<T>(this T item, List<T> list)
{
    list.Add(item);
}

With that the first code block can be reduced to:

var someList = new List<SomeType>();
anotherList.Find(somePredicate)?.AddTo(someList);

But this solution is specific to this example (i.e. adding an object to a list if it's not null). Is there a general way to indicate that if a parameter is null, the method should not be run?

Garey answered 8/4, 2018 at 8:57 Comment(0)
T
4

Never do this

var someList = new List<SomeType>();
if (anotherList.Find(somePredicate) != null)
{
    someList.Add(anotherList.Find(somePredicate))
}

this will search the list twice which is unnecessary. use a temporary variable instead.

var someList = new List<SomeType>();
var find = anotherList.Find(somePredicate);
if (find != null) someList.Add(find);

Is there a general way to indicate that if a parameter is null, the method should not be run?

Currently there is no such feature. how ever there are other alternatives that work better like in other answers provided.

Tepefy answered 8/4, 2018 at 9:21 Comment(1)
Thank you for pointing that out! The first block of code was meant to be an example of undesirable code. One of the reasons why I wanted something like the null-conditional operator is because it evaluates the object only once without having to explicitly declare a temporary variable.Garey
Z
4

You could also use just:

var someList = new List<SomeType>();
someList.AddRange(anotherList.Where(somePredicate));

In your case you probably need to make sure that your predicate only finds at max one element.

Zsigmondy answered 8/4, 2018 at 9:13 Comment(3)
good solution. you can use Take(1) to only pick the first element.Tepefy
I guess if you use Take(1), you should also use OrderBy(predicate) - otherwise the result may be arbitrary.Zsigmondy
Adding a possibly null object to a list was just an example for my question about coming up with a shorthand for possibly null parameters in general, but thank you for the suggestion! It was very informative.Garey
T
4

Never do this

var someList = new List<SomeType>();
if (anotherList.Find(somePredicate) != null)
{
    someList.Add(anotherList.Find(somePredicate))
}

this will search the list twice which is unnecessary. use a temporary variable instead.

var someList = new List<SomeType>();
var find = anotherList.Find(somePredicate);
if (find != null) someList.Add(find);

Is there a general way to indicate that if a parameter is null, the method should not be run?

Currently there is no such feature. how ever there are other alternatives that work better like in other answers provided.

Tepefy answered 8/4, 2018 at 9:21 Comment(1)
Thank you for pointing that out! The first block of code was meant to be an example of undesirable code. One of the reasons why I wanted something like the null-conditional operator is because it evaluates the object only once without having to explicitly declare a temporary variable.Garey
M
2

TLDR: Not yet.

The team plans to implement a functionality into the language, that would do this exact thing. It would look something like this:

someList.Add(anotherList.Find(somePredicate) ?? return);

In which the ?? return checks whether the first argument is null, and if it is, than it returns. (Or you could write break if that is what you want)

I am not sure if this is a thing they are working on at the moment, and if it will be included in the next version of C#, but would go a long way if it is going to be.

Marou answered 8/4, 2018 at 9:8 Comment(1)
this will not work if this is not the last line in your method. you may want to continue the method instead of returning from it. I believe break would only work inside loops.Tepefy

© 2022 - 2024 — McMap. All rights reserved.