https://msdn.microsoft.com/zh-cn/library/z883w3dc(v=vs.80).aspx

List 中会保留集合中元素的顺序。

如果新的 Count(当前 Count 加上集合的大小)大于 Capacity,则会通过自动重新分配内部数组增大 List 的容量以容纳新元素,并在添加新元素之前将现有元素复制到新数组中。

如果 List 可以在不增加 Capacity 的情况下容纳新元素,则此方法是 O(n) 运算,其中 n 是要添加的元素数。如果需要增加此容量以容纳新元素,则此方法变为 O(n + m) 运算,其中 n 是要添加的元素数,m 是 Count

List.AddRange :将指定集合的元素添加到 List 的末尾。

using System;
using System.Collections.Generic; public class Example
{
public static void Main()
{
string[] input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" }; List<string> dinosaurs = new List<string>(input); Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\nAddRange(dinosaurs)");
dinosaurs.AddRange(dinosaurs); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\nRemoveRange(2, 2)");
dinosaurs.RemoveRange(, ); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} input = new string[] { "Tyrannosaurus",
"Deinonychus",
"Velociraptor"}; Console.WriteLine("\nInsertRange(3, input)");
dinosaurs.InsertRange(, input); Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
string[] output = dinosaurs.GetRange(, ).ToArray(); Console.WriteLine();
foreach( string dinosaur in output )
{
Console.WriteLine(dinosaur);
}
}
} /* This code example produces the following output: Capacity: 3 Brachiosaurus
Amargasaurus
Mamenchisaurus AddRange(dinosaurs) Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus RemoveRange(2, 2) Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus InsertRange(3, input) Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus output = dinosaurs.GetRange(2, 3).ToArray() Amargasaurus
Tyrannosaurus
Deinonychus
*/

c# List AddRange的更多相关文章

  1. Add和AddRange的区别

    Add和AddRange区别 Add和AddRange Add:将指定的对象添加到……中 AddRange:向……末尾,添加数组 - 在群体操作时,使用AddRange取代Add 用AddRange可 ...

  2. C# AddRange为数组添加多个元素的代码

    将代码过程中重要的代码片段做个收藏,下面代码段是关于C# AddRange为数组添加多个元素的代码,希望对小伙伴有所用处.ArrayList ab = new ArrayList();ab.Add(& ...

  3. HttpWebRequest.AddRange 支持long类型

    很久很久以前,在哪个FAT32格式还流行的年代,文件大小普遍还没超过4G的年代,.Net已经出来了. 而那时候.Net实现的HTTP断点续传协议,还没预料到如此普及(我猜的).那时候的HttpWebR ...

  4. AddRange 取代 Add

    cmd.Parameters的Add方法是增加一个参数,增加多个参数的的时候使用一个foreach循环,cmd.Parameters的AddRange方法是增加一个参数的数组. Add:将指定的对象添 ...

  5. Add和AddRange的使用

    Add 是每次将单个元素添加到集合里面 AddRange可以一次性添加多个元素到集合里面 AddRange例子:         public static int ExecuteCommand(st ...

  6. 【译】第42节---EF6-DbSet.AddRange & DbSet.RemoveRange

    原文:http://www.entityframeworktutorial.net/entityframework6/addrange-removerange.aspx EF 6中的DbSet引入了新 ...

  7. 批量 1 insert into select 2 sqldataadapter.update 3 SELECT INTO FROM (要求目标表不存在) 4 AddRange(ef) 5 MySqlBulkLoader.Load() 6 BatchInsert 7 insert bulk

     insert into a(col1,col2) select top 1 '1','2' from a union select top 1 '3','4' from a    SELECT IN ...

  8. C#集合中的Add与AddRange方法

    C#.NET的集合主要位于System.Collections和System.Collections.Generic(泛型)这两个namespace中. 1.System.Collections 比如 ...

  9. AutoDetectChangesEnabled及AddRange解决EF插入的性能问题

    转自:http://www.cnblogs.com/nianming/archive/2013/06/07/3123103.html#2699851 记录下. 园友莱布尼茨写了一篇<Entity ...

随机推荐

  1. 【BZOJ-1692&1640】队列变换 后缀数组 + 贪心

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1153  Solved: 482[Submit][St ...

  2. codeforces 21D:Traveling Graph

    Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...

  3. PHP Lex Engine Sourcecode Analysis(undone)

    catalog . PHP词法解析引擎Lex简介 . PHP标签解析 1. PHP词法解析引擎Lex简介 Relevant Link: 2. PHP标签解析 \php-5.4.41\Zend\zend ...

  4. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  5. OC冒泡排序

    NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"3",@"5",@"4&q ...

  6. 我总结的js性能优化的小知识

    前言 一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己 ...

  7. iOS - 沙盒规范

    1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件 ...

  8. 捉襟见肘之message sent to deallocated instance 0x16f62a70

    出现的问题(真机ios8到ios9测试没有问题,真机ios7.1出现问题): -- :::60b] *** -[ChatViewController scrollViewDidScroll:]: me ...

  9. Fix failed to start session in Ubuntu

    When you are at login, press Ctrl+Alt+F1. It will take you to command line interface from the GUI. I ...

  10. 常用机器视觉工具----图像分析工具(blob分析)

    http://blog.sina.com.cn/s/blog_67cc4eb70100ivnt.html Blob分析:Blob分析目的在于对图像中的2-D形状进行检测和分析,得到诸如目标位置.形状. ...