0

I want to know how can check list object is not null before using ForEach loop. Below is example code which I am trying:

List<string> strList   ;
strList.ForEach (x => Console.WriteLine(x)) ;

I looking for a solution in terms of lambda expression and do not want to use if statement.

12
  • 3
    You should avoid having a null list in the first place. Whatever is generating the list should simply return an empty list, instead of null. Commented Apr 16, 2014 at 14:44
  • @GrantWinney You don't have to. It's the best solution, but not the only solution. Commented Apr 16, 2014 at 14:47
  • Are you worried that strList is null, or that an element in strList is null? Commented Apr 16, 2014 at 14:51
  • 1
    FYI - you can change that to strList.ForEach(Console.WriteLine), since Console.WriteLine is already a method that takes a string and returns void. Commented Apr 16, 2014 at 14:54
  • 2
    strList?.ForEach (x => Console.WriteLine(x)) ; should work Commented Aug 8, 2017 at 17:07

3 Answers 3

2

You can write an extension method for List<>, which will check for null and otherwise it will call ForEach on its this parameter. Call it ForEachWithNullCheck or something like this and you will be fine.

public static void ForEachWithNullCheck<T>(this List<T> list, Action<T> action)
{
  if (list == null)
  {
    // silently do nothing...
  }
  else
  {
    list.ForEach(action); 
  }
}

Usage example:

List<string> strList;
strList.ForEachWithNullCheck(x => Console.WriteLine(x));
Sign up to request clarification or add additional context in comments.

Comments

-1

The most correct/idiomatic solution (if you cannot avoid having a null collection to begin with) is to use an if:

if(list != null)
    foreach(var str in list)
        Console.WriteLine(str);

Putting the if into a lambda isn't going to make anything any easier. In fact, it'll only create more work.

Of course if you really hate using an if you can avoid it, not that it'll really help you much:

foreach(var str in list??new List<string>())
    Console.WriteLine(str);

foreach(var str in list == null ? new List<string>() : list)
    Console.WriteLine(str);

You could emulate the more functional style Maybe concept with a method that invokes an action if the object isn't null, not that this is really any easier than a null check when dealing with actions instead of functions:

public static void Maybe<T>(this T obj, Action<T> action)
{
    if (obj != null)
        action(obj);
}

strList.Maybe(list =>
    foreach(var str in list)
        Console.WriteLine(str));

Comments

-1

You might have already got a better solution. Just wanted to show how I did it.

List<string> strList   ;
strList.ForEach (x => string.IsNullOrEmpty(x)?Console.WriteLine("Null detected"): Console.WriteLine(x)) ;

In my scenario I am summing up a value in a foreach as shown below.

double total = 0;
List<Payment> payments;
payments.ForEach(s => total += (s==null)?0:s.PaymentValue);

1 Comment

This does handle if an element of the list is null, but I don't think it's handling if the list itself is null. I get the null error on the ForEach itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.