加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:52:45 所属栏目:百科 来源:网络整理
导读:假设我有一个对象列表TheListOfObjects. 如果我写这个: TheListOfObjects = TheListOfObjects.Take(40).ToList(); 如果列表中只有30个项目还是只返回前30个项目,它会崩溃吗?当TheListOfObjects为空时,甚至是null? 谢谢. 解决方法 这是您应该尝试或至少检
假设我有一个对象列表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并润滑了您的车轮一点点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读