linq相关问题

发布于 2024-10-20 16:50:52 字数 2414 浏览 3 评论 0原文

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("---------------------");
        }
    }

}
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("---------------------");
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-10-27 16:50:52

调用 .ToList() 强制急切执行完整枚举 - 其结果是与原始枚举分开的列表,因此 .ToList( ) 不会反映在该列表中。此列表中的实际项目与@Johannes Rudolph 指出的原始枚举中的相同(相同的对象引用) - 所以是的,这是一个浅副本。

不过,IEnumerable 将会在源集合上延迟执行 - 仅当您主动枚举项目时(即通过使用 foreach 或 < code>.ToList()) 枚举将创建一个枚举器,该枚举器采用当时的源集合 - 这意味着如果在创建枚举器之前基础集合中存在更改,这些更改将得到反映在枚举中。

Calling .ToList() forces eager execution of the full enumeration - the result of this is a separate list from the original enumeration, so any changes after .ToList() won't be reflected in that list. The actual items in this list are the same (same object references) as in the original enumeration as @Johannes Rudolph pointed out - so yes that's a shallow copy.

The IEnumerable<test> though will be lazily executed over the source collection - only when you actively enumerate the items (i.e. by using foreach or .ToList()) will the enumeration create an Enumerator that takes the source collection as it is at that point in time - that means if there are changes in the underlying collection before the Enumerator is created these will be reflected in the enumeration.

凤舞天涯 2024-10-27 16:50:52

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

请参阅 MSDN

ToList will always create a shallow copy of your list, that is the returned list will reference the same objects as your source IEnumerable does, but the List returned is a copy of the source.

See MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文