【问题标题】:linq related questionlinq相关问题
【发布时间】:2011-03-08 22:37:38
【问题描述】:
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("---------------------");
        }
    }

}

【问题讨论】:

    标签: c# linq deep-copy


    【解决方案1】:

    调用.ToList() 会强制eager 执行完整的枚举 - 结果是与原始枚举不同的列表,因此.ToList() 之后的任何更改都不会反映在该列表中.这个列表中的实际项目与@Johannes Rudolph 指出的原始枚举中的相同(相同的对象引用) - 所以是的,这是一个浅拷贝。

    虽然IEnumerable&lt;test&gt; 将在源集合上延迟执行 - 只有当您主动枚举项目(即使用foreach.ToList())时,枚举才会创建一个枚举器在那个时间点获取源集合 - 这意味着如果在创建 Enumerator 之前基础集合中有更改,这些将反映在枚举中。

    【讨论】:

      【解决方案2】:

      ToList 将始终创建列表的浅表副本,即返回的列表将引用与源 IEnumerable 相同的对象,但返回的列表是来源。

      MSDN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 2011-07-31
        • 2013-01-09
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多