微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 当集合数小于参数时取(参数)

假设我有一个对象列表ThelistofObjects.

如果我写这个:

ThelistofObjects = ThelistofObjects.Take(40).ToList();

如果列表中只有30个项目还是只返回前30个项目,它会崩溃吗?当ThelistofObjects为空时,甚至是null?

谢谢.

解决方法

这是您应该尝试或至少检查 documentation的那些之一.

Will it crash if there are only 30 items in the list or will it just return the first 30?

它将返回前30个.

And when ThelistofObjects is empty

它将返回空序列.

or even null?

它将导致ArgumentNullException.

来自MSDN:

Take<TSource> enumerates source and yields elements until count elements have been yielded or source contains no more elements.

If count is less than or equal to zero,source is not enumerated and an empty IEnumerable<TSource> is returned.

在例外情况下:

Exception             Condition
ArgumentNullException source is null

在您登录StackOverflow时,您可以检查MSDN并获得权威答案(请注意,这里已经发布并删除了一些轻微不正确或部分不完整的答案)或者启动了Visual Studio并润滑了您的车轮一点点.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐