出处:博客园

作者:mumuliang

连接:http://www.cnblogs.com/mumuliang/p/3914425.html

Capacity

在.NET中List的容量应该只是受到硬件限制。

属性Capacity的真正含义,是创建List时给它预分配的容量。

一旦项的数量超过了当前的Capacity,Capacity会以

Capacity = (Capacity==0)?A_VALUE_YOU_CANNOT_SURE:(Capacity*2)

的方式增长。

当使用默认构造函数创建一个List时,Capacity就是0。(或者你非要用一个0作为capacity参数调用构造)

所以当Capacity是0时,我们为List添加了第一个项目后,Capacity是多少呢?

对,是A_VALUE_YOU_CANNOT_SURE

不过,在.NET2.0和.NET4.5中,这个值看上去都是4。

            System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity));

            int initCapacity = 0;
List<string> l = new List<string>(initCapacity);
System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity)); for (int u = 0; u < 65; ++u)
{
System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity));
//l.TrimExcess();
//System.Diagnostics.Debug.WriteLine(string.Format(" TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity));
l.Add("1");
}

输出

default capacity is :
constructe a list with capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:
Count: , Capacity:

TrimExcess()

TrimExcess()的意思就是,如果Capacity大于Count,清掉哪些没有使用的空间。基本上可以理解成让Capacity等于Count,只是基本上,不能信赖这一点。.NET毕竟还要考虑很多性能问题。TrimExcess method does nothing if the list is at more than 90 percent of capacity——这就是.NET的方式。实际上把上面那段代码中的注释放开,就会看到确实存在Capacity和Count不等的情况(然而,并不是90%哟)。Capacity不够以后的增长规则,仍然是*2,并不会因为被Trim过就只是加1。

放开注释后的输出

default capacity is :
constructe a list with capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:

发现并不是90%以后

System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity));

int initCapacity = ;
List<string> l = new List<string>(initCapacity);
System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity)); int initCount = ;
for (int u = ; u < initCount; ++u)
{
l.Add(u.ToString());
} for (int u = ; u < initCount; ++u)
{
System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity));
l.TrimExcess();
System.Diagnostics.Debug.WriteLine(string.Format(" TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity));
l.Remove(u.ToString());
}

得到

default capacity is :
constructe a list with capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:
Count: , Capacity:
TrimExcess() -> Count: , Capacity:

貌似条件是酱紫的

if( count<(int)Math.Floor((double)capacity*0.9) )

do trim

linq之Capacity(转载)的更多相关文章

  1. linq to sql转载

    LINQ简介 LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库 ...

  2. 30分钟LINQ教程 【转载】

    原文地址:http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html 在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有 ...

  3. 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)

    转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...

  4. 【转载】ADO.NET与ORM的比较(3):Linq to SQL实现CRUD

    [转载]ADO.NET与ORM的比较(3):Linq to SQL实现CRUD 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibernate ...

  5. 转载一篇文章:LINQ TO SQL 大全

    https://www.cnblogs.com/chenwolong/p/lts.html 最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河 ...

  6. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  7. SQL,LINQ,Lambda语法对照图(转载)

    如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...

  8. LINQ to SQL更新数据库操作(转载)

    使用LINQ to SQL建模Northwind数据库 在这之前一起学过LINQ to SQL设计器的使用,下面就使用如下的数据模型: 当使用LINQ to SQL设计器设计以上定义的五个类(Prod ...

  9. 转载linq to sql 的详解

    [转]LINQ To SQL 语法及实例大全 2011-11-26阅读38651 评论9 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL ...

随机推荐

  1. Android手机中怎么样在没root的情况下 修改 hosts 文件

    工具  链接:https://pan.baidu.com/s/1AENluDCQ-2qYDPcE5K6l8g 密码:t7eu  http://bbs.360.cn/forum.php?mod=view ...

  2. 洛谷P4559 [JSOI2018]列队(主席树)

    题面 传送门 题解 首先考虑一个贪心,我们把所有的人按\(a_i\)排个序,那么排序后的第一个人到\(k\),第二个人到\(k+1\),...,第\(i\)个人到\(k+i-1\),易证这样一定是最优 ...

  3. mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server

    mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server 如果你想连接你的mysql的时候发生这个错误: ERROR 1 ...

  4. TFS解锁命令

    tf undo  /server:http://193.100.100.29:8080/tfs/EGSS  /workspace:HY-PC;hy   $/CTCS/TSAGS_TEAM/TestHY ...

  5. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  6. c语言和c++的相互调用

    1.c与c++编译方式 (1)gcc和g++都可以编译.c文件,也都可以编译.cpp文件.g++和gcc是通过后缀名来辨别是c程序还是c++程序的(这一点与Linux辨别文件的方式不同,Linux是通 ...

  7. 不带 www 跳转 到 带 www 网站..

    IIS: <rule name="已导入的规则 1-1" stopProcessing="true"> <match url="^( ...

  8. POJ2248 Addition Chains 迭代加深

    不知蓝书的标程在说什么,,,,于是自己想了一下...发现自己的代码短的一批... 限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索 #include<cstdio> ...

  9. A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【数学】

    题目: Little C loves number «3» very much. He loves all things about it. Now he has a positive integer ...

  10. Pseudoprime numbers---费马小定理

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13406   Accepted: 5 ...