简体   繁体   English

为什么我使用IList()。toList()。AddRange()有错误结果?

[英]Why i have error result by use IList().toList().AddRange()?

Those are my old Code that cannot response correct result. 这些是我的旧代码,无法响应正确的结果。

IList<string> testList=new List<string>();
testList.Add("string1");
var testList2=new List<string>(testList);
testList.ToList().AddRange(testList2);

I expect there are two elements in testList,but in fact it only have one; 我希望testList中有两个元素,但实际上它只有一个; If i change my to code to new style ,its can get the right result.Example : 如果我将代码更改为新样式,则可以得到正确的结果。

IList<string> testList=new List<string>();
testList.Add("string1");
var testList2=new List<string>(testList);
var result=testList.ToList();
result.AddRange(testList2);

In result,it successfully have two elements.I guess the reason is iList().toList() create a new List in other place,as param result,that is independent from iList(). 结果,它成功地包含两个元素。我猜想原因是iList()。toList()在其他位置创建了一个新列表,作为参数结果,它独立于iList()。 Is it right? 这样对吗? or other reasons? 还是其他原因?

All the extension methods that come with System.Linq namespace create a new copy of the object (deep copy, if you will). System.Linq命名空间随附的所有扩展方法都将创建对象的新副本(深层副本,如果可以的话)。 So, you are right when you say that a new list is created at another memory location. 因此,当您说在另一个存储位置创建了一个新列表时,您是对的。

To confirm whether two objects are different, you can get their memory addresses via unsafe and see for yourself. 要确认两个对象是否不同,可以通过不安全的方式获取它们的内存地址,然后自己查看。 Here is something to test (be cautious if you are using unsafe code; you need to handle all the memory management yourself): 这是要测试的东西(如果使用不安全的代码,请谨慎;您需要自己处理所有内存管理):

unsafe
            {
                IList<string> testList = new List<string>();
                testList.Add("string1");
                var testList2 = new List<string>(testList);
                testList.ToList().AddRange(testList2);

                TypedReference original = __makeref(testList);
                IntPtr originalPointer = **(IntPtr**)(&original);

                var isThisANewList = testList;

                TypedReference newReferenceOnly = __makeref(testList);
                IntPtr newReferenceOnlyPointer = **(IntPtr**)(&newReferenceOnly);

                var copy = testList.ToList();

                TypedReference deepCopy = __makeref(copy);
                IntPtr deepCopyPointer = **(IntPtr**)(&deepCopy);
            }

ToList() is an extension method that returns a list. ToList()是返回列表的扩展方法。 You aren't holding that value in a variable, so although you call AddRange() , you are adding to the list you created in ToList() not to your testList. 您没有在变量中保留该值,因此尽管您调用AddRange() ,但您将添加到在ToList()创建的列表中,而不是添加到testList中。

In your second example, you are correctly holding the value of ToList() in result. 在第二个示例中,您正确地保留了结果ToList()的值。

var result=testList.ToList();
result.AddRange(testList2);

You have a couple of options... 您有几种选择...

// option 1 declare testList as a List not IList
List<string> testList = new List<string>();
testList.AddRange(testList2);

// option 2 cast testList as a List
((List<string>)testList).AddRange(testList2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将IList转换为List以进行AddRange - Trying to convert IList to List in order to AddRange 为什么当我不使用清单时datetime.now不起作用? - why datetime.now not work when I didn't use tolist? 我看不到使用ToList(); 或在此select语句中使用(6)个项目 - I cant seen to use ToList(); or take(6) items on this select statement LINQ ToList()序列化错误? - LINQ ToList() serialization error? 如果多次使用,我是否要缓存FindControl()结果? - Have I to cache FindControl() result if I use it a number of times? 在Linq和Exe Time中使用ToList() - Use ToList() in Linq and Exe Time 为什么查询结果在sql-server运行中可以,但是在ASP.Net页面中使用查询时却出错? - Why result of a query is OK in sql-server run, but has an error when I use it in a ASP.Net page? 为什么我有参数对象时需要使用$? - Why do i need to use the $ when i have a parameter object? 为什么我在Webform网站上遇到此错误内部服务器错误500 - Why I have this error on webform site Internal server error 500 如果我必须每次都等待而不是使用Sync .ToList(),使用.ToListAsync的各个多数据库查询是否有任何性能优势? - Is there any performance benefit in respective multiple db query with .ToListAsync if i have to await each time rather than using Sync .ToList()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM