collections

How to declare a method that returns a generic collection of "anything" (C#)

I am using a hierarchy of generic collection classes that derive from an abstract base class to store entity items that also derive from an abstract base class: abstract class ItemBase { } class MyItem : ItemBase { public MyItem() { } } abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { } class ...

type of system.Collection <T>

I'm creating a list of <T>, but i want to know the type of that T-object. (i'm using reflection in my project, so i don't know the type when i'm creating my code. So first i have my List --> List<T> values Now i want to get all the properties of that T-object (for creating columns in a datagrid) any help? ...

Does the Enumerator of a Dictionary<TKey, TValue> return key value pairs in the order they were added? .Net

Hi, I understand that a dictionary is not an ordered collection and one should not depend on the order of insertion and retrieval in a dictionary. However, this is what I noticed: Added 20 key value pairs to a Dictionary Retrieved them by doing a foreach(KeyValuePair...) The order of retrieval was same as the order in which they we...

Better to use a list of pairs, or two lists?

I'm writing a method that forms part of the public interface of a Java class. It broadly allows the caller to specify the values to assign to a number of database entities - so they must supply both the IDs of the entities themselves, and the values to assign to them. I'm wavering between implementing this as a List<Pair<Integer, Integ...

Best way of storing very large number of objects in memory?

Very simple question, what would be your way of storing 100 KB - 2 MB objects in memory? Object is made of 3 doubles and two strings (both mostly under 5 chars long). Would using struct instead of class be any better? EDIT: I don't know why I said double, it is float .. :S ...

Adding IEnumerable<T> to class derived from CollectionBase

Suppose I have a class TestCollection which is used to hold objects of type Test and is defined as public class TestCollection : CollectionBase This allows me to iterate through the collection either as foreach(object o in collection) ... or foreach(Test t in collection) but does not allow me to use new Linq queries. If I cha...

Generic Methods in C#

Generic Methods in general are new to me. Need a method that returns a Collection of a generic type, but also takes a collection of the same generic type and takes Expression<Func<GenericType, DateTime?>>[] Dates parameter. T throughout the following function should be the same type, so right now I was using (simplified version): ...

Asp.net MVC Binding an object with properties which are collections of objects which also have child collection properties

Hi, I'm trying to retreive data from a massive form using Asp.net MVC. Its top object L1 contains properties which are Collections of other types L2. However the Type L2 contains some properties which collections of type L3, and so on. There are probably 5 levels of nested collections. I've seen the approach of binding to Lists in Asp....

AddRange to a Collection

There are some similar questions to this; I did not find one that I felt answered this use case. (Please feel free to correct me!) A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection<T>. There's a get-only property of that type that already contains some items. He wants to add the i...

How to create Public String property with drop-down list of options?

Is it possible to attach a List of strings to a String property so that the user can select one of the strings from the Properties window? Should I implement ICollection or something of that sort? ...

Is there a built-in more elegant way of filtering-and-mapping a collection by element type?

If I want to narrow, say, an Iterable[A] for all elements of a particular type (e.g. String) I can do: as filter { _.isInstanceOf[String] } However, it's obviously desirable to use this as an Iterable[String] which can be done via a map: as filter { _.isInstanceOf[String] } map { _.asInstanceOf[String] } Which is pretty ugly. Of co...

Is there a List<T> in .NET 2 that raises events when the list changes?

I have used ObservableCollection<T> in the past, but that seems to belong to WPF and therefore .NET 3. And if there isn't what would be the appropriate interface for that? INotifyPropertyChanged seems not to be a very good fit for collections, while INotifyCollectionChanged is again only supported in .NET 3 and higher. ...

java generics and collection assignment

If I have this class: class Foo<T> implements SomeInterface { final private List<T> list = new ArrayList<T>(); final private Class<? extends T> runtimeClass; public Foo(Class<? extends T> cl) { this.runtimeClass = cl; } // method override from SomeInterface @Override public boolean addChild(Object o) { ...

How to best handle List<Entity>, List<Entity Id>, List<Entity name> efficiently?

I have a java application which uses JPA. Say I have an entity called Product with name and price attributes (all entities have an id attribute). Naturally I can get a List<Product> fairly easily (from a query or another entity), but often I want a List<String> (list of product names) or List<Long> (list of product prices or list of pr...

Converting non-generic List type to Generic List type in Java 1.5

I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List<ObjectType> based on the incoming List object so that my calling code is talking to List<ObjectType>. What's the best way to convert the List (or any other object collect...

Filter large colection

Hi there, I have a collection of MyClass objects and i need to filter it by a combination of 17 fileds. I implemented an object MyClassFilter with the 17 possible fields and the condition for each one and a method: bool PassFilter(MyClass ObjectToEvaluate) { return PassFilterVal(this.Workstream, ObjectToEvaluate.WorkStream) && ...

Retrieving Client's Printer Collection in asp.net

Hello, I am working on web application .net 3.5,asp.net,C#. In that i need to provide list of available printer at client side. I have knowledge of how to get server side printers using [1] System.Drawing.Printing.PrinterSettings.InstalledPrinters [2] System.Management.ManagementObjectSearcher [using query "SELECT * FROM Win32_Prin...

How to implement a multi-index dictionary?

Basically I want something like Dictionary<Tkey1, TKey2, TValue>, but not (as I've seen here in other question) with the keys in AND, but in OR. To better explain: I want to be able to find an element in the dictionary providing just one of the keys, not both. I also think we should consider thread-safety and the ability to easily scale...

Hibernate/Ehcache: evicting collections from 2nd level cache not synchronized with other DB reads

I have an application using JPA, Hibernate and ehcache, as well as Spring's declarative transactions. The load on DB is rather high so everything is cached to speed things up, including collections. Now it is not a secret that collections are cached separately from the entities that own them so if I delete an entity that is an element of...

how to get the one entry from hashmap without iterating

Is there a elegant way of obtaining only one Entry<K,V> from HashMap, without iterating, if key is not known. As order of entry of entry is not important, can we say something like hashMapObject.get(zeroth_index); Although I am aware that there exist no such get by index method. If I tried approach mentioned below, it would still ha...