ienumerable

Sortedset not using custom equals

流过昼夜 提交于 2020-01-07 07:24:13
问题 My class implements IEquatable and IComparable. It then gets added to a sortedset. The goal is to have it be sorted by its "Date" property and be equal if both "ID1" and "ID2" are the same. The implemented methods: public int CompareTo(MyClass other) { return other.Date.CompareTo(Date); } public override int GetHashCode() { unchecked { var hashCode = ID1; hashCode = (hashCode * 397) ^ ID2; return hashCode; } } public bool Equals(MyClass other) { if (ReferenceEquals(null, other)) return false;

Missing directory and file info

旧时模样 提交于 2020-01-06 21:09:47
问题 The following is not returning a full list of files and directories: IEnumerable<FileSystemInfo> files = new DirectoryInfo("C:\\Windows\\System32\\drivers").EnumerateFileSystemInfos("*", SearchOption.AllDirectories); The application is run as a administrator. There are no additional file filter drivers in place. For example if I run: foreach (FileSystemInfo file in files) { Console.WriteLine(file.Name); } I get: en-US gm.dls gmreadme.txt UMDF wimmount.sys bfe.dll.mui ndiscap.sys.mui pacer.sys

Missing directory and file info

☆樱花仙子☆ 提交于 2020-01-06 21:07:34
问题 The following is not returning a full list of files and directories: IEnumerable<FileSystemInfo> files = new DirectoryInfo("C:\\Windows\\System32\\drivers").EnumerateFileSystemInfos("*", SearchOption.AllDirectories); The application is run as a administrator. There are no additional file filter drivers in place. For example if I run: foreach (FileSystemInfo file in files) { Console.WriteLine(file.Name); } I get: en-US gm.dls gmreadme.txt UMDF wimmount.sys bfe.dll.mui ndiscap.sys.mui pacer.sys

Generic single flag enumeration for bitmask enums

℡╲_俬逩灬. 提交于 2020-01-05 04:09:28
问题 I'm working on a codebase with several bit flags enums which look something like this public enum BitMask { None = 0, OptionA = 1 << 0, OptionB = 1 << 1, OptionAandB = OptionA | OptionB, All = ~0 } I can iterate over all enum values using this public IEnumerable<T> EnumValues<T>() { return Enum.GetValues(typeof(T)).Cast<T>(); } I'm looking for a generic way to iterate over single flag values only, in this case, OptionA and OptionB . Not None , OptionAandB , All . I can cast to long and detect

How can I dump the normal properties on an IEnumerable in Linqpad

房东的猫 提交于 2020-01-04 04:21:11
问题 If I have an object that among other things is an IEnumerable and I dump this object I get the enumerated values. Is there a way to get Linqpad to list the other properties: Se example below: Can I get Dump to include Hello and digits properties? void Main() { var t = new test(); var d = new Dictionary<string,string> {{"Hello","World"},{"Good by","Sky"}}; t.Dump(); d.Dump(); } // Define other methods and classes here public class test : IEnumerable { public string Hello { get { return "World"

Tell LINQ Distinct which item to return

落花浮王杯 提交于 2020-01-04 02:34:25
问题 I understand how to do a Distinct() on a IEnumerable and that I have to create an IEqualityComparer for more advanced stuff however is there a way in which you can tell which duplicated item to return? For example say you have a List<T> List<MyClass> test = new List<MyClass>(); test.Add(new MyClass {ID = 1, InnerID = 4}); test.Add(new MyClass {ID = 2, InnerID = 4}); test.Add(new MyClass {ID = 3, InnerID = 14}); test.Add(new MyClass {ID = 4, InnerID = 14}); You then do: var distinctItems =

How FirstOrDefault extension method works?

随声附和 提交于 2020-01-04 02:28:05
问题 I was wondering on how FirstOrDefault extension method works? Which one of the following algorithms does it follows? Use: var arr = new[] {1, 2, 3, 4, 5, 6, 7}; return arr.FirstOrDefault(x => x%2 == 0); Algorithm 1: for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) return arr[i]; } return 0; Algorithm 2: var list = new List<int>(); for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) list.Add(arr[i]); } return list.Count == 0 ? 0 : list[0]; Does the FirstOrDefault algorithm is

Implementing multiple IEnumerables in C#

喜夏-厌秋 提交于 2020-01-04 01:59:08
问题 I have a generic class, Class<T> , that implements IEnumerable<T> . T is also constrained to implement IConvertable . I also want this class to be able to pretend to be a string-like object, so I want to implement IEnumerable<char> . However, IEnumerable<T> and IEnumerable<char> collide -- what happens if T is char ? Does anyone have any suggestions on how to accomplish this? EDIT: Here's some clarification -- I'd like to be able to do the following: public IEnumerator<T> GetEnumerator() {

Trying to implement a method that can compare any two lists but it always returns false

元气小坏坏 提交于 2020-01-03 19:35:49
问题 I'm trying to make a method that can compare any two lists for equality. I'm trying to compare them in a way that validates that every element of one list has the same value as every element of another list. My Equals method below always returns false , can anyone see why that is? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class IEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>> { public bool Equals

Trying to implement a method that can compare any two lists but it always returns false

て烟熏妆下的殇ゞ 提交于 2020-01-03 19:35:11
问题 I'm trying to make a method that can compare any two lists for equality. I'm trying to compare them in a way that validates that every element of one list has the same value as every element of another list. My Equals method below always returns false , can anyone see why that is? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class IEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>> { public bool Equals