简体   繁体   English

IEnumerable object 是否需要检查 null?

[英]Is null checking required for IEnumerable object?

var selectedRows = from drow in ugTable.Rows
                         .Cast<Infragistics.Win.UltraWinGrid.UltraGridRow>()
                         .Where(drow => drow != null && drow.Selected) 
                   select drow;

if(selectedRows.Count()==1){//do something with selected rows}

From the above statement, do i need to check Null for the selectedRows variable?从上面的语句中,我需要检查 Null 的 selectedRows变量吗? selectedRows is an IEnumerable variable. selectedRows 是一个 IEnumerable 变量。

You do not need to check if selectedRows is null .您不需要检查selectedRows是否为null The returned IEnumerable<> might be empty, but it will never be null .返回的IEnumerable<>可能为空,但绝不会是null

As an aside, I'd suggest you simplify your code by writing:顺便说一句,我建议您通过编写以下代码来简化代码:

var selectedRows
    = ugTable.Rows.Cast<Infragistics.Win.UltraWinGrid.UltraGridRow>()
                  .Where(drow => drow != null && drow.Selected);

Which is shorter and equivalent.哪个更短且等效。

The LINQ query will return an empty list (0 items), if there are no matches on the where.如果 where 没有匹配项,LINQ 查询将返回一个空列表(0 项)。

So, no need to check for null .因此,无需检查null

In your example you'll be fine using the extension method.在您的示例中,您可以使用扩展方法。 But if you were to implement your own method that returned an IEnumerable, the answer depends on how you return your result.但是,如果您要实现自己的返回 IEnumerable 的方法,则答案取决于您返回结果的方式。

The following method returns an empty enumerable:以下方法返回一个空的可枚举:

IEnumerable<object> Correct()
{
    yield break;
}

The following method just returns null:以下方法只返回 null:

IEnumerable<object> Incorrect()
{
    return null;
}

Calling these methods will give the following results:调用这些方法将给出以下结果:

Correct().Any(); // returns false
Incorrect().Any(); // throws ArgumentNullException

So be careful when you return IEnumerable.所以当你返回 IEnumerable 时要小心。 Try to use the yield keyword and following the correct pattern.尝试使用 yield 关键字并遵循正确的模式。

My initial feeling is no, you don't but it certainly can't hurt.我最初的感觉是不,你没有,但它肯定不会受伤。

I have, from I think Phil Haack, a useful extension method that checks to see if an IEnumerable is null or empty...我认为 Phil Haack 有一个有用的扩展方法,它检查IEnumerable是 null 还是空的......

    /// <summary>
    /// Determines whether the collection is either null or empty.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source collection.</param>
    /// <returns>
    ///     <c>true</c> if the collection is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
    {
        return source == null || !source.Any();
    }

.Any() is much more efficient for checking if not empty than .Count() .Any() .Count()比 .Count() 更有效地检查是否为空

Linq wont retrun NULL. Linq 不会重新运行 NULL。 If you want to check some data is there you can go with Any()如果你想检查一些数据,你可以用Any() go

var selectedRows = from drow in ugTable.Rows
                         .Cast<Infragistics.Win.UltraWinGrid.UltraGridRow>()
                         .Where(drow => drow != null && drow.Selected) 
                   select drow;
if(selectedRows .Any())
{
//your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM