gpt4 book ai didi

c# - 奇怪,IEnumerable.ToList() 创建了全新的对象

转载 作者:太空狗 更新时间:2023-10-29 17:52:02 25 4
gpt4 key购买 nike

我知道 IEnumerable.ToList() 应该创建一个新列表,但项目指向 IEnumerable 中相同的原始项目,如 ToList()-- Does it Create a New List? 中所述。

但是,我使用 VS 2012 的代码出现了一些奇怪的行为;工作框架;和.NET 4.0。当 IEnumerable.SequenceEquals() 似乎没有按我预期的那样工作时,它就开始了。我仔细研究了我的 QuickWatch 对话框,令人难以置信的是,以下语句的计算结果为 false:

this.Items.First () == this.Items.ToList ()[ 0 ]

我什至试过:

this.Items.ToList ().IndexOf(this.Items.First ())

评估为 -1。

Items 被声明为 WPF 自定义控件的属性,如下所示:

public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register (
"Items",
typeof ( IEnumerable<UserLayoutType> ),
typeof ( UserLayoutSelectorControl ),
new FrameworkPropertyMetadata ( null, FrameworkPropertyMetadataOptions.AffectsRender, UserLayoutSelectorControl.PropertyChanged ) );


public IEnumerable<UserLayoutType> Items
{
get
{
return ( IEnumerable<UserLayoutType> ) this.GetValue ( UserLayoutSelectorControl.ItemsProperty );
}
set
{
this.SetValue ( UserLayoutSelectorControl.ItemsProperty, value );
}
}

UserLayoutType 只是一个由 XSD 工具生成的类,声明如下:

// 
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
//
namespace MyAssays.UserLayoutCore.UserLayoutUtility {
using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("UserLayout", Namespace="", IsNullable=false)]
public partial class UserLayoutType {

这是工厂类中首先创建 UserLayoutType 项的方法:

public static IEnumerable<UserLayoutType> CreateFromFolder ( string folderPath )
{
if (String.IsNullOrEmpty(folderPath))
throw new ArgumentNullException("folderPath", "Folder path must not be null");

var userLayoutXmlFilePaths = Directory.GetFiles ( folderPath ).Where ( filePath => filePath.EndsWith ( ".UserLayout.xml", StringComparison.InvariantCultureIgnoreCase ) );
return userLayoutXmlFilePaths.Select(filePath => UserLayoutFactory.CreateFromFile(filePath));
}

public static UserLayoutType CreateFromFile ( string filePath )
{
using ( var stream = new StreamReader ( filePath ) )
{
return ( UserLayoutType ) new XmlSerializer ( typeof ( UserLayoutType ) ).Deserialize ( stream );
}
}

有人知道发生了什么事吗?见下图: enter image description here

最佳答案

您从中看到新对象的主要原因可能是 IEnumerable<T>正在包装一个生成器,而不是一个具体化的集合。

这是一个简单的 LINQPad程序演示:

void Main()
{
IEnumerable<string> collection =
from index in Enumerable.Range(1, 10)
select "Index=" + index;

var list1 = collection.ToList();
var list2 = collection.ToList();

ReferenceEquals(list1[0], list2[0]).Dump();
}

这将打印 False .

之所以这样做,是因为枚举集合的行为(在本例中为 .ToList())将执行延迟的 LINQ 查询,并且由于我们枚举集合两次,所以我们执行两次,产生不同的实例相同的值。

关于c# - 奇怪,IEnumerable.ToList() 创建了全新的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com