ienumerable

Why does this string extension method not throw an exception?

痴心易碎 提交于 2019-12-18 10:09:37
问题 I've got a C# string extension method that should return an IEnumerable<int> of all the indexes of a substring within a string. It works perfectly for its intended purpose and the expected results are returned (as proven by one of my tests, although not the one below), but another unit test has discovered a problem with it: it can't handle null arguments. Here's the extension method I'm testing: public static IEnumerable<int> AllIndexesOf(this string str, string searchText) { if (searchText =

IEnumerable.Select with index

橙三吉。 提交于 2019-12-18 07:44:28
问题 I have the following code: var accidents = text.Skip(NumberOfAccidentsLine + 1).Take(numberOfAccidentsInFile).ToArray(); where accidents is an array of strings. I want to make a Linq transformation from the string array to an array of Accident objects as follows: return accidents.Select(t => new Accident() {Id = i, Name = t.Replace("\"", string.Empty)}).ToArray(); How do I retrieve the index i from the accidents array using Linq or do I have to go old school? 回答1: I'm not sure what kind of

How do I skip default JavaScript array serialization for IEnumerable types in Json.Net?

[亡魂溺海] 提交于 2019-12-18 04:47:16
问题 Some custom types that implement IEnumerable don't necessarily have backing collections. They could be generated dynamically, for example using 'yield' or LINQ. Here is an example: public class SOJsonExample { public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>> { public List<int> Keys { get; set; } public List<float> Values { get; set; } public MyCustomEnumerable() { Keys = new List<int> { 1, 2, 3 }; Values = new List<float> { 0.1f, 0.2f, 0.3f }; } public IEnumerator

Why does XmlSerializer require types which inherit from IEnumerable to have an implementation of Add(System.Object)?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 04:10:16
问题 I am using xml serialization but now came across a runtime error I haven't seen before. "To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. ImageEditor.EffectOptions does not implement Add(System.Object)" It seems a little weird to be forced to implement a method via runtime exception, rather than compile time error such as missing methods from implemented interfaces. Is this by design?

Recommended way to check if a sequence is empty

微笑、不失礼 提交于 2019-12-17 23:30:58
问题 A method returns a sequence, IEnumerable<T> , and you now want to check if it is empty. How do you recommend doing that? I'm looking for both good readability and good performance. The first and most obvious way is to check that the count is greater than zero: if(sequence.Count() == 0) Has decent readability, but terrible performance since it has to actually go through the whole sequence. A method that I sometimes use is the following: if(!sequence.Any()) This doesn't (as far as I know) have

Difference between IEnumerable and IEnumerable<T>?

有些话、适合烂在心里 提交于 2019-12-17 23:03:23
问题 What is the difference between IEnumerable and IEnumerable<T> ? I've seen many framework classes implementing both these interfaces, therefore I would like to know what advantages one get by implementing both? Please have a look how they've been defined: public interface IEnumerable { [DispId(-4)] IEnumerator GetEnumerator(); } public interface IEnumerable<T> : IEnumerable { IEnumerator<T> GetEnumerator(); } As we see, IEnumerable<T> derives from IEnumerable , that means whatever IEnumerable

C#: How can I make an IEnumerable<T> thread safe?

半城伤御伤魂 提交于 2019-12-17 21:56:00
问题 Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0; while(n < 100) yield return n++; } How would you make this thread safe? And by that I mean that you would get that enumerator once, and have multiple threads handle all the numbers without anyone getting duplicates. I suppose a lock needs to be used somewhere, but where must that lock be for an iterator block to be thread safe? What, in general, do you need to remember if you want a thread safe IEnumerable<T> ?

Practical difference between List and IEnumerable [duplicate]

∥☆過路亽.° 提交于 2019-12-17 21:48:46
问题 This question already has answers here : IEnumerable vs List - What to Use? How do they work? (10 answers) Closed 6 years ago . By reading similar posts I've learned that a List is a type of IEnumerable. But I'm really wondering what the practical difference between those two actually is. To someone who always have used a List and never used IEnumerable: What is the practical difference between the two? In what scenarios is one of them better than the other? Here is a practical example : We

Translation of yield into VB.NET

心已入冬 提交于 2019-12-17 20:27:17
问题 first i must assume that i'm not very familiar with the C# yield keyword and its function. What is the best/easiest way to "translate" it into VB.NET? Especially i tried to convert this code into VB.NET, but i failed with: yield return new MatchNode(++index, current.Value); What i have is: Imports System.Collections Imports System.Data.SqlTypes Imports System.Diagnostics.CodeAnalysis Imports System.Text.RegularExpressions Imports Microsoft.SqlServer.Server Class MatchNode Private _index As

Can't add/remove items from a collection while foreach is iterating over it

霸气de小男生 提交于 2019-12-17 19:53:51
问题 If I make my own implementation of IEnumerator interface, then I am able ( inside foreach statement )to add or remove items from a albumsList without generating an exception.But if foreach statement uses IEnumerator supplied by albumsList , then trying to add/delete ( inside the foreach )items from albumsList will result in exception: class Program { static void Main(string[] args) { string[] rockAlbums = { "rock", "roll", "rain dogs" }; ArrayList albumsList = new ArrayList(rockAlbums);