Tag: ienumerable

Class是否需要实现IEnumerable才能使用Foreach

这是在C#中,我有一个类,我从其他人的DLL使用。 它没有实现IEnumerable,但有2个传递IEnumerator的方法。 有没有办法可以在这些上使用foreach循环。 我正在使用的课程是密封的。

为什么Enumerable.Range比直接yield循环更快?

下面的代码是检查执行相同解决方案的三种不同方式的性能。 public static void Main(string[] args) { // for loop { Stopwatch sw = Stopwatch.StartNew(); int accumulator = 0; for (int i = 1; i accumulator + n); sw.Stop(); Console.WriteLine(“time = {0}; result = {1}”, sw.ElapsedMilliseconds, ret); } //self-made IEnumerable { Stopwatch sw = Stopwatch.StartNew(); var ret = GetIntRange(1, 100000000).Aggregate(0, (accumulator, n) => accumulator + […]

如何将DataTable转换为动态对象?

如何在IEnumerable转换DataTable ? 例如,我想转换任何 DataTable ID | Name DI | emaN ——— or ——— 1 | x 2 | x 2 | y 1 | y 在对象列表中 // list 1 (ex 1) // list 2 (ex 2) { { { ID = 1, Name = “x” } { DI = 2, emaN = “x” } { […]

Skip的性能(以及类似function,如Take)

我刚看了.NET Framework的Skip / Take扩展方法的源代码(在IEnumerable类型上),发现内部实现正在使用GetEnumerator方法: // .NET framework public static IEnumerable Skip(this IEnumerable source, int count) { if (source == null) throw Error.ArgumentNull(“source”); return SkipIterator(source, count); } static IEnumerable SkipIterator(IEnumerable source, int count) { using (IEnumerator e = source.GetEnumerator()) { while (count > 0 && e.MoveNext()) count–; if (count <= 0) { while (e.MoveNext()) yield return […]

LINQ并设置差异

我有两个集合a和b 。 我想计算a或b的项集,但不能同时计算(逻辑异或)。 使用LINQ,我可以想出这个: IEnumerable Delta(IEnumerable a, IEnumerable b) { return a.Except (b).Union (b.Except (a)); } 我想知道是否还有其他更有效或更紧凑的方法来产生两个集合之间的差异。 编辑1:Jon Skeet发布了第一个解决方案,它不依赖于HashSet保留项目的顺序。 我想知道是否还有其他方法可以保留输出中a和b的顺序。

为什么实现变体接口的类保持不变?

C#4.0进一步扩展了通用类型和接口的协方差和逆变。 一些接口(如IEnumerable )是协变量,所以我可以这样做: IEnumerable ie = new List(); 但是这条线怎么样? 我遇到了编译时错误 List list = new List(); //Cannot implicitly convert type List’ to List’ 我的意思是,如果List实现IEnumerable为什么List仍然是不变的? 有一个很好的反例可以解释为什么在C#中不允许这样做?

枚举本质上不是IEnumerable的集合?

当你想递归枚举一个分层对象,根据一些标准选择一些元素时,有许多技术的例子,比如“展平”,然后使用Linq进行过滤:如下所示: 链接文字 但是,当你枚举类似Form的Controls集合或TreeView的Nodes集合时,我一直无法使用这些类型的技术,因为它们似乎需要一个参数(对于扩展方法),这是一个IEnumerable集合:传入SomeForm.Controls不编译。 我发现最有用的是: 链接文字 这为您提供了Control.ControlCollection的扩展方法,其中包含IEnumerable结果,然后您可以使用Linq。 我修改了上面的例子来解析TreeView的节点没有问题。 public static IEnumerable GetNodesRecursively(this TreeNodeCollection nodeCollection) { foreach (TreeNode theNode in nodeCollection) { yield return theNode; if (theNode.Nodes.Count > 0) { foreach (TreeNode subNode in theNode.Nodes.GetNodesRecursively()) { yield return subNode; } } } } 这是我现在使用扩展方法编写的代码: var theNodes = treeView1.Nodes.GetNodesRecursively(); var filteredNodes = ( from n in theNodes where […]

当从List inheritance类时,XmlSerializer不会序列化其他属性

我在这里有一个情况,我需要从Listinheritance我的类,但是当我这样做时,XmlSerializer不会序列化我的类中声明的任何属性或字段,以下示例演示: public partial class Form1 : Form { public Form1() { InitializeComponent(); DoSerialize(); } private void DoSerialize() { MyClass obj = new MyClass(); obj.Add(1); obj.Add(2); obj.Add(3); XmlSerializer s = new XmlSerializer(typeof(MyClass)); StringWriter sw = new StringWriter(); s.Serialize(sw, obj); } } [Serializable] [XmlRoot] public class MyClass : List { public MyClass() { } int myAttribute = […]

关于洗刷所需的IEnumerable的扩展方法

我需要一个扩展IEnumerable的扩展方法。 它还可以使用int来指定返回的IEnumerable的大小。 更好地保持IEnumerable不变性。 我目前的IList解决方案 – public static IList Shuffle(this IList list, int size) { Random rnd = new Random(); var res = new T[size]; res[0] = list[0]; for (int i = 1; i < size; i++) { int j = rnd.Next(i); res[i] = res[j]; res[j] = list[i]; } return res; } public static IList Shuffle(this […]