using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Comparer.csd
{
class Program
{
/* Confusion is regarding how ToList() method works. Does it do the deep copy or shallow copy??
/**********OUTPUT
a a b
a b c
a c a
-----------------
a a b
a c a
-----------------
1
2
3
OUTPUT ENDS************/
static void Main(string[] args)
{
List<test> list = new List<test>();
list.Add(new test("a", "b", "c"));
list.Add(new test("a", "c", "a"));
list.Add(new test("a", "a", "b"));
/// sort the list based on first name and last name
IEnumerable<test> soretedCollection = from t in list
orderby t._fname ascending, t._mname ascending
select t;
Print(soretedCollection);
/// remove the first object from the list
list.RemoveAt(0);
/// print the list .
/// Removal of the item from the list is reflected here so I guess sorted collection and list both
/// are refering the same data structure
/// what if I will do
/// list = soretedCollection.ToList<test>(); /// is it going to change the reference of the list if some other object
/// is holding the reference??
Print(soretedCollection);
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(1, 1);
dic.Add(2, 1);
dic.Add(3, 1);
List<int> keys = dic.Keys.ToList<int>();
/// remove the dictionary entry with key=2
dic.Remove(2);
/// how come this time it did not remove the second item becuase it is removed from the dictionary.
for (int i = 0; i < keys.Count; ++i)
{
Console.WriteLine(keys[i].ToString());
}
Console.Read();
}
static void Print(IEnumerable<test> list)
{
foreach (test t in list)
{
t.Print();
}
Console.WriteLine("---------------------");
}
}
}
Q
LINQ相关问题
0
A
回答
4
调用.ToList()
力量渴望执行完整枚举的 - 这样做的结果是从原来的枚举一个单独的列表,所以.ToList()
后的任何更改将不在该列表中得到反映。这个列表中的实际项目与@Johannes Rudolph指出的原始枚举中的相同(相同的对象引用) - 所以是的,这是一个浅拷贝。
的IEnumerable<test>
虽然将懒洋洋地在源集合执行 - 只有当你积极地列举项目将枚举创建一个枚举取源集合,因为它是在这一点上(通过使用foreach
或.ToList()
IE)及时 - 这意味着如果在创建枚举器之前底层集合中存在更改,这些将会反映在枚举中。
3
ToList
将始终创建列表的浅表副本,这是返回的列表将引用相同对象作为源IEnumerable
做,但返回的列表是源代码的副本。
参见MSDN。
相关问题
- 1. 堆相关问题
- 2. viewWillAppear相关问题
- 3. TableLayout相关问题
- 4. Pearson相关问题
- 5. stackoverflow“相关问题”
- 6. SSL相关问题
- 7. StringBuilder相关问题
- 8. MVC相关问题
- 9. StringBuilder相关问题
- 10. setContentsURL相关问题
- 11. jquery相关问题
- 12. 与C++相关的问题有问题
- 13. DOM相关的问题和问题
- 14. 与LINQ和对象引用相关的问题
- 15. 按列表中的问题排序(linq相关)
- 16. Cython - gil相关问题
- 17. Kohana链接相关问题
- 18. 与@property相关的问题
- 19. 与IndexOutOfRangeException相关的问题
- 20. 复制相关问题,
- 21. Stackoverflow的相关问题
- 22. 与UIButton相关的问题
- 23. JQGrid,搜索相关问题
- 24. SQL连接相关问题
- 25. 的JavaScript相关问题
- 26. Windows DPI相关问题
- 27. 问题与相关实体
- 28. 相关选择框问题
- 29. SQL Server相关问题
- 30. MySQL查询相关问题:
完美...懂了...谢谢 – mchicago 2011-03-09 03:31:50