开发者

Would this LINQ statement ever return null?

Given the following C# code:

开发者_开发技巧List<string> source = new List<string>();

IEnumerable<string> values = from value in source select value;

Will values ever be null or will it always return an empty sequence?


Yes it CAN return null if you have an extension method defined in your code somewhere like the following:

public static IEnumerable<string> Select(this List<string> list, Func<string, string> action)
{
    return null;
}

Otherwise no; it will return an empty sequence.


The values sequence itself will never be null. If source is empty then values will be an empty sequence containing no items.

(And, of course, it's possible that one or more of the string items in the sequence might be null.)


Linq returns empty sequences If you want to test if the sequence is empty use the .Any() method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜