为什么通用列表AddRange返回void而不是列表? [英] Why does the Generic List AddRange return void instead of a list?

查看:52
本文介绍了为什么通用列表AddRange返回void而不是列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace System.Collections.Generic
  public List<T>    
    public void AddRange(IEnumerable<T> collection)

似乎不返回任何东西可能是一个故意的设计决定.我对这个期望AddRange是流利的"感到ten恼.

It seems like it might be an intentional design decision not to return something. I got bitten by this expecting AddRange to be "fluent."

我只是好奇是否有人知道设计动机,如果有的话什么都不退?

I was just curious if anyone knew of the design motivations, if there were any, to return nothing?

推荐答案

.NET 2.0中在LINQ推出之前,以及当流利"确实不流行时,在.NET 2.0中添加了 AddRange 方法.完全没有.当时,没有人期望 AddRange 返回任何内容.另外,在该版本的.NET中,没有List对象的初始化程序,因此您必须将一堆项目添加()到列表中.AddRange提供了一种无需遍历和逐个添加对象的快捷方式.

The AddRange method was added in .NET 2.0, before LINQ was out, and when "fluency" really wasn't in fashion at all. At the time, no one expected AddRange to return anything. Plus, in that version of .NET, there was no initializer for List object, so you had to Add() a bunch of items to the list. The AddRange provided a shortcut for not having to loop through and Add objects one by one.

等效的LINQ方法是 IEnumerable< T> .Concat()

The equivalent LINQ method is IEnumerable<T>.Concat()

这篇关于为什么通用列表AddRange返回void而不是列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆