ilist

Best string container: StringCollection, Collection<string>, List<string>, ArrayList, ..?

和自甴很熟 提交于 2019-11-28 06:17:16
What is the most suitable container just for strings holding in some array with non-predetermined upper boundary, which length is unknown on it's creation. For simple code like: var list = new SomeContainer(); // size is unknown for()/foreach()/do()/while() // any loop { list.Add(string); } Is it StringCollection as optimized Collection for string, or just Collection<string> or List<string> or ArrayList ? What is the different between them? For what you need, List<string> is probably the most versatile. StringCollection was handy in 1.1, when (without generics) you needed specific classes for

C# Syntax - Split String into Array by Comma, Convert To Generic List, and Reverse Order

こ雲淡風輕ζ 提交于 2019-11-28 04:15:28
What is the correct syntax for this: IList<string> names = "Tom,Scott,Bob".Split(',').ToList<string>().Reverse(); What am I messing up? What does TSource mean? The problem is that you're calling List<T>.Reverse() which returns void . You could either do: List<string> names = "Tom,Scott,Bob".Split(',').ToList<string>(); names.Reverse(); or: IList<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>(); The latter is more expensive, as reversing an arbitrary IEnumerable<T> involves buffering all of the data and then yielding it all - whereas List<T> can do all the reversing "in

Why is there no IArray(T) interface in .NET?

自作多情 提交于 2019-11-27 23:35:48
Update 2011-Jan-06: Believe it or not, I went ahead and incorporated this interface into an open source library I've started, Tao.NET . I wrote a blog post explaining this library's IArray<T> interface, which not only addresses the issues I originally raised in this question (a year ago?!) but also provides a covariant indexed interface , something that's sorely lacking (in my opinion) in the BCL. Question (in short): I asked why .NET has IList<T> , which implements ICollection<T> and therefore provides methods to modify the list ( Add , Remove , etc.), but doesn't offer any in-between

What should I use an IEnumerable or IList? [duplicate]

空扰寡人 提交于 2019-11-27 20:50:20
问题 This question already has an answer here: IList vs IEnumerable for Collections on Entities 2 answers Can anyone tell me when I should use either. For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct? Thank you. 回答1: Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList ( IList implements IEnumerable ) so unless you want something specific from

Why doesn't Array class expose its indexer directly?

好久不见. 提交于 2019-11-27 15:44:06
问题 something to mention for answering: Don't worry about variance , while the item in question is Array rather than T[] . A similar case for multi-dimension arrays is [here] That is, N-dims to linear transform, is always possible. So this question especially caught my attention, since it already implemented IList for a linear indexer. Question: In my code, I have following declaration: public static Array ToArray<T>(this T source); My code knows how to make souce presents an array(at runtime).

DataGridView Using SortableBindingList

一个人想着一个人 提交于 2019-11-27 05:16:36
I have a function that returns an IList< T > and is the DataSource for a DataGridView. I learned that DataGridView won't sort IList. I read This stackoverflow Q&A and am trying to implement SortableBindingList. I must be doing something wrong because my DataGridView is empty. I also tried to access an element from the SortableBindingSource with a TextBox and nothing as well. using Microsoft.SqlServer.Management.Controls; public partial class Form1 : Form { IBusinessLayer businessLayer; IList<Category> categories; SortableBindingList<Category> catSortable; public Form1() { InitializeComponent()

Implementing IList interface

血红的双手。 提交于 2019-11-27 02:43:41
问题 I am new to generics. I want to implement my own collection by deriving it from IList<T> interface. Can you please provide me some link to a class that implements IList<T> interface or provide me a code that at least implements Add and Remove methods? 回答1: Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection<T> since it has everything you need. Please note that although implementors of IList<T> are not required to

Best string container: StringCollection, Collection<string>, List<string>, ArrayList, ..?

那年仲夏 提交于 2019-11-27 01:15:49
问题 What is the most suitable container just for strings holding in some array with non-predetermined upper boundary, which length is unknown on it's creation. For simple code like: var list = new SomeContainer(); // size is unknown for()/foreach()/do()/while() // any loop { list.Add(string); } Is it StringCollection as optimized Collection for string, or just Collection<string> or List<string> or ArrayList ? What is the different between them? 回答1: For what you need, List<string> is probably the

Cast IList to List

倖福魔咒の 提交于 2019-11-27 00:50:49
问题 I am trying to cast IList type to List type but I am getting error every time. List<SubProduct> subProducts= Model.subproduct; Model.subproduct returns IList<SubProduct> . 回答1: Try List<SubProduct> subProducts = new List<SubProduct>(Model.subproduct); or List<SubProduct> subProducts = Model.subproducts as List<SubProduct>; 回答2: How about this: List<SubProduct> subProducts = Model.subproduct.ToList(); 回答3: In my case I had to do this, because none of the suggested solutions were available:

Sorting an IList in C#

一世执手 提交于 2019-11-26 21:57:44
So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it. Turns out the IList interface doesn't have a sort method built in. I ended up using the ArrayList.Adapter(list).Sort(new MyComparer()) method to solve the problem but it just seemed a bit "ghetto" to me. I toyed with writing an extension method, also with inheriting from IList and implementing my own Sort() method as well as casting to a List but none of these seemed overly elegant. So my question is, does anyone have an elegant solution to sorting an