Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用ForEach lambda表达式之前检查列表对象不为null_C#_Linq - Fatal编程技术网

C# 如何在使用ForEach lambda表达式之前检查列表对象不为null

C# 如何在使用ForEach lambda表达式之前检查列表对象不为null,c#,linq,C#,Linq,在使用ForEach循环之前,我想知道如何使checklist对象不为null。下面是我正在尝试的示例代码: List<string> strList ; strList.ForEach (x => Console.WriteLine(x)) ; 列表strList; strList.ForEach(x=>Console.WriteLine(x)); 我正在寻找lambda表达式方面的解决方案,不想使用if语句。最正确/惯用的解决方案(如果不能)是使用if: if(li

在使用ForEach循环之前,我想知道如何使checklist对象不为null。下面是我正在尝试的示例代码:

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

我正在寻找lambda表达式方面的解决方案,不想使用
if
语句。

最正确/惯用的解决方案(如果不能)是使用
if

if(list != null)
    foreach(var str in list)
        Console.WriteLine(str);
if
放入lambda不会使任何事情变得更容易。事实上,这只会创造更多的工作

当然,如果你真的不喜欢使用
if
,你可以避免它,但这并不是说它对你有多大帮助:

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


您可以为
列表
编写一个扩展方法,该方法将检查null,否则它将对其
参数调用
ForEach
。称它为ForEachWithNullCheck或类似的东西,你会没事的

public static void ForEachWithNullCheck<T>(this List<T> list, Action<T> action)
{
  if (list == null)
  {
    // silently do nothing...
  }
  else
  {
    list.ForEach(action); 
  }
}
publicstaticvoidforeachwithnullcheck(此列表,操作)
{
if(list==null)
{
//默默地什么也不做。。。
}
其他的
{
列表.ForEach(行动);
}
}
用法示例:

List<string> strList;
strList.ForEachWithNullCheck(x => Console.WriteLine(x));
列表strList;
strList.ForEachWithNullCheck(x=>Console.WriteLine(x));

您可能已经找到了更好的解决方案。只是想展示一下我是怎么做到的

List<string> strList   ;
strList.ForEach (x => string.IsNullOrEmpty(x)?Console.WriteLine("Null detected"): Console.WriteLine(x)) ;
列表strList;
strList.ForEach(x=>string.IsNullOrEmpty(x)?Console.WriteLine(“检测到Null”):Console.WriteLine(x));
在我的场景中,我对foreach中的一个值求和,如下所示

double total = 0;
List<Payment> payments;
payments.ForEach(s => total += (s==null)?0:s.PaymentValue);
双倍合计=0;
列出付款清单;
payments.ForEach(s=>total+=(s==null)?0:s.PaymentValue;

首先应避免使用空列表。无论生成列表的是什么,都应该返回一个空列表,而不是
null
@grantwiney,您不必这样做。这是最好的解决方案,但不是唯一的解决方案。您是否担心
strList
为null,或者
strList
中的元素为null?仅供参考-您可以将其更改为
strList.ForEach(Console.WriteLine)
,因为
Console.WriteLine
已经是一个接受字符串并返回void的方法。
strList?.ForEach(x=>Console.WriteLine(x));
应该有效如果列表的元素为null,它会处理,但是如果列表本身为null,我认为它不会处理。我在ForEach本身上得到null错误。
List<string> strList;
strList.ForEachWithNullCheck(x => Console.WriteLine(x));
List<string> strList   ;
strList.ForEach (x => string.IsNullOrEmpty(x)?Console.WriteLine("Null detected"): Console.WriteLine(x)) ;
double total = 0;
List<Payment> payments;
payments.ForEach(s => total += (s==null)?0:s.PaymentValue);