generic-collections

Why doesn't Collections.Generic.Queue have Synchronized method but Collections.Queue has?

纵饮孤独 提交于 2019-12-22 05:53:18
问题 System.Collections.Queue class has Queue.Synchronized method which returns a thread-safe Queue implementation. But the generic one, System.Collections.Generic.Queue does not have a Synchronized method. At this point I have two questions in mind: Why doesn't generic one have this method? Is it a framework API design decision? How is the queue returned from Queue.Synchronized is different than ConcurrentQueue<T> class? Thanks. 回答1: The Synchronized() method returns a wrapper queue that slaps a

How can I return <TEnumerable, T> : where TEnumerable:IEnumerable<T>

自作多情 提交于 2019-12-22 05:34:16
问题 Goal: Generic enumerated type to be the same type when returned. Note: This works when the types are entered but I don't understand why they can't be inferred. List<T> then return List<T> IOrderedEnumerable<T> then return IOrderedEnumerable<T> ETC Current method (works only if all types are entered) public static TEnumerable WithEach<TEnumerable, T>(this TEnumerable items, Action<T> action) where TEnumerable : IEnumerable<T> { foreach (var item in items) action.Invoke(item); return items; }

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

我的梦境 提交于 2019-12-21 15:42:50
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

六眼飞鱼酱① 提交于 2019-12-21 15:42:45
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

和自甴很熟 提交于 2019-12-21 15:42:35
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!

Limit the size of a generic collection?

陌路散爱 提交于 2019-12-18 13:25:09
问题 Is there any way to limit the size of a generic collection? I have a Stack of WriteableBitmap which I am using to store a clone of a WriteableBitmap on each change, meaning that I can undo easily by simply Popping the most recent WriteableBitmap off the stack. The problem is the memory usage, I want to limit this stack to hold 10 objects, but I can't see a property allowing me to easily do this. Is there a way, or am I going to have to check the stack size on each change, and copy the last 10

C# / .NET equivalent for Java Collections.<T>emptyList()?

為{幸葍}努か 提交于 2019-12-18 11:41:05
问题 What's the standard way to get a typed, readonly empty list in C#, or is there one? ETA: For those asking "why?": I have a virtual method that returns an IList (or rather, post-answers, an IEnumerable ), and the default implementation is empty. Whatever the list returns should be readonly because writing to it would be a bug, and if somebody tries to, I want to halt and catch fire immediately, rather than wait for the bug to show up in some subtle way later. 回答1: Personally, I think this is

Generic Types Collection

我的梦境 提交于 2019-12-18 05:23:10
问题 Building on previous question which got resolved, but it led to another problem. If protocol/class types are stored in a collection, retrieving and instantiating them back throws an error. a hypothetical example is below. The paradigm is based on "Program to Interface not an implementation" What does it mean to "program to an interface"? instantiate from protocol.Type reference dynamically at runtime public protocol ISpeakable { init() func speak() } class Cat : ISpeakable { required init() {

Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

怎甘沉沦 提交于 2019-12-17 16:59:36
问题 What does it mean? I used a list of list in ASP.NET MVC and sent them through ViewData of ActionResuls to retrieve it in views. However, when I change it to list of list, it gives me an error of HttpWebException . When I check it inside the immediate window, it tells me that the error is: Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments So, what does it mean and what did I do wrong using it? 回答1: The List<T> class is a generic type. In order to use it, you

What is a custom collection?

て烟熏妆下的殇ゞ 提交于 2019-12-13 19:26:46
问题 A Group of objects. However i am having confusion in the following case. A sample class class A { public string Foo{get; set;} } class A_list { public A[] list; public A_list(A[] _list) { list = new A[_list.Length]; for (int i = 0; i < _list.Length; i++) { list[i] = _list[i]; } } } static void Main(String[] args) { A[] names = new A[3] { new A{Foo="some"}, new A{Foo="another"}, new A{Foo="one"} }; A_list just_an_object = new A_list(names); } Which of the above is a custom collection the array