generic-collections

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

≯℡__Kan透↙ 提交于 2019-12-04 07:40:20
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! public interface IOne { string One(); } public interface ITwo : IOne { string Two(); } public interface

How to remove from List<T> efficiently (C#)?

痴心易碎 提交于 2019-12-03 15:51:04
If I understood correctly (and please correct me if i'm wrong), list is implemented by array in .NET, which means that every deletion of an item in the list will cause re-allocation of all the list (which in turn means O(n) ). I'm developing a game, in the game i have many bullets fly in the air on any giving moment, let's say 100 bullets, each frame I move them by few pixels and check for collision with objects in the game, I need to remove from the list every bullet that collided. So I collect the collided bullet in another temporary list and then do the following: foreach (Bullet bullet in

Type-safe generic containers with macros

自古美人都是妖i 提交于 2019-12-01 05:36:36
I'm trying to make a type-safe generic linked list in C using macros. It should work similarly to how templates work in C++. For example, LIST(int) *list = LIST_CREATE(int); My first attempt was for #define LIST(TYPE) (the macro I used above) to define a struct _List_##TYPE {...} . That, however, did not work because the struct would be redefined every time I declared a new list. I remedied the problem by doing this: /* You would first have to use this macro, which will define the `struct _List_##TYPE`... */ DEFINE_LIST(int); int main(void) { /* ... And this macro would just be an alias for

Type-safe generic containers with macros

浪尽此生 提交于 2019-12-01 04:05:12
问题 I'm trying to make a type-safe generic linked list in C using macros. It should work similarly to how templates work in C++. For example, LIST(int) *list = LIST_CREATE(int); My first attempt was for #define LIST(TYPE) (the macro I used above) to define a struct _List_##TYPE {...} . That, however, did not work because the struct would be redefined every time I declared a new list. I remedied the problem by doing this: /* You would first have to use this macro, which will define the `struct

Generic types : wildcards vs variables of raw types

冷暖自知 提交于 2019-12-01 00:38:27
Consider following methods: public static void listAll(LinkedList list) { for(Object obj : list) System.out.println(obj); } and public static void listAll(LinkedList<?> list) { for(Object obj : list) System.out.println(obj); } What is the difference between these two methods? If there is no difference, why we should use the second one? <?> doesn't allow you to add objects in list. See the program below. It is specific type of list we have passed to method <?> . Specific means, list was created with specific type and passed to <?> method listAll . Don't confuse with word specific . Specific can

Generic types : wildcards vs variables of raw types

大憨熊 提交于 2019-11-30 19:33:02
问题 Consider following methods: public static void listAll(LinkedList list) { for(Object obj : list) System.out.println(obj); } and public static void listAll(LinkedList<?> list) { for(Object obj : list) System.out.println(obj); } What is the difference between these two methods? If there is no difference, why we should use the second one? 回答1: <?> doesn't allow you to add objects in list. See the program below. It is specific type of list we have passed to method <?> . Specific means, list was

Limit the size of a generic collection?

痞子三分冷 提交于 2019-11-30 09:26:52
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 objects into a new stack whenever I hit 10, and on each subsequent change? I know how to do this, but

Can't add keyValuePair directly to Dictionary

橙三吉。 提交于 2019-11-29 09:04:38
I wanted to add a KeyValuePair<T,U> to a Dictionary<T, U> and I couldn't. I have to pass the key and the value separately, which must mean the Add method has to create a new KeyValuePair object to insert, which can't be very efficient. I can't believe there isn't an Add(KeyValuePair<T, U>) overload on the Add method. Can anyone suggest a possible reason for this apparent oversight? Backup a minute...before going down the road of the oversight, you should establish whether creating a new KeyValuePair is really so inefficient. First off, the Dictionary class is not internally implemented as a

Generic Types Collection

大兔子大兔子 提交于 2019-11-29 08:52:47
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() {} func speak() { println("Meow"); } } class Dog : ISpeakable { required init() {} func speak() {

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

旧城冷巷雨未停 提交于 2019-11-28 02:04:52
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? The List<T> class is a generic type. In order to use it, you need to provide a type argument. For example, if you want to have a list of integers, you need to declare