generics

C# type parameters specification

Some special CLI types from mscorlib library (ArgIterator, TypedReference and RuntimeArgumentHandle types) cannot be used as generic type parameters to construct the generic types / methods: void Foo<T>() { } void Bar() { Foo<ArgIterator>(); } provides the compiler error: error CS0306: The type 'System.ArgIterator' may not be used as...

How to iterate through google collections' Multimap<?, Object> ?

Before using google collections I had something similar to next code: private Set<A> aSet = ...; private Set<B> bSet = ...; public Foo getFoo (Map<?, List<Bar>> bars, Set<?> set) { for (Object item : set) { for (Bar bar : bars.get (item)) { //build foo; } } ... } and I was able to make calls like these:...

Mixing type parameters and abstract types in scala

I am trying to use the answer of a preceding question to implement a small graph library. The idea is to consider graphs as colections, where vertices wrap collection elements. I would like to use abstract types to represent Vertex and Edge types (because of type safety) and I want to use type parameters to represent the type of the col...

Can I declare a variable of Type<T> without specifying T at compile time?

How do I Load the class "MyContent" dynamically ? I have 1 interface<T>, 1 abstract generic class<T> and 1 class. Check my code out: public interface IMyObjectInterface{ } public abstract MyAbstractObject : IMyObjectInterface{ } public class MyObject : MyAbstractObject{ } public interface IMyContentInterface<T> where T : MyAbstractObj...

How does a STL multimap differ from a .NET Dictionary<key, List<values>> ?

I have a problem where I need a .NET dictionary that supports multiple items per key. In the past I've used the STL multimap in my C++ programs. How does the design of a multimap differ from a dictionary of lists i.e. performance, size, etc. (excluding generics vs. templates)? ...

How to convert this piece of code to Generics?

I have the following extension method that takes a List and converts it to a comma separated string: static public string ToCsv(this List<string> lst) { const string SEPARATOR = ", "; string csv = string.Empty; foreach (var item in lst) csv += item + SEPARATOR; // remove the trailing...

C# (Generic of a Generic)??

Is there any way to express this idea in C#? (Basically a generic type of a generic type)? public static class ExtensionSpike { public static IEnumerable<T> Where<TCollection<T>>(this TCollection<T> sourceCollection, Expression<Func<T, bool>> expr) where TCollection : class, IEnumerable<T>, INotifyCollectionChanged { ...

How to reference current class type using generics

I have a base class, with a method, where I would like to use generics to force the coder to use a generic expression on the current class: public class TestClass { public void DoStuffWithFuncRef<T>(Expression<Func<T, object>> expression) where T : TestClass { this.DoStuffWithFuncRef(Property<T>.NameFor(expressio...

Generics and sorting in Java

Suppose you write a static function in Java to sort an array, much like Arrays.sort(). The problem with Arrays.sort() is that it receives an array of Object, and throws a ClassCastException if its elements don't implement Comparable. So you want your function to receive as an argument an array of a subtype of Comparable. Something like ...

Wiring up generic interfaces in StructureMap

There's a few questions on SO about StructureMap and generics, and I've read a few blog posts about it, but still I'm struggling to figure out the solution to this particular scenario. Given: public interface ILookup { } public interface ISupplier : ILookup { } public interface ITenant : ILookup { } public class Supplier : ISupplier...

Specify a C# Generic Constraint to be of ClassA OR ClassB?

Is there a way to specify that a generic type be of one type or another type? public class SoftDrink<T> where T : TypeOne or TypeTwo { } ...

C# Convert ArrayList Custom Sort to Generic List<T> Custom Sort

Hi, I have an ArrayList of objects that I need to sort in two different fashions, depending on the situation. I followed this example: http://codebetter.com/blogs/david.hayden/archive/2005/03/06/56584.aspx on how to create a PersonComparer by overloading the IComparer object. I liked this method because it allowed me to build an enum...

Java generics question - Class<T> vs. T?

I'm using Hibernate validator and trying to create a little util class: public class DataRecordValidator<T> { public void validate(Class<T> clazz, T validateMe) { ClassValidator<T> validator = new ClassValidator<T>(clazz); InvalidValue[] errors = validator.getInvalidValues(validateMe); [...] } } Questio...

C# Generic List of Generic List of Multiple Types

Here is an abstraction and simplification of my issue: I have a set of toys and a corresponding box for these toys. I want the user to be able to specify the largest type of toy that the box can hold: public class Box<T> {} then within the Box class I want to have a generic list of toys, but each toy contained within the box will hav...

C# Dynamic Generic Type

Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround? Type k = typeof(double); List<k> lst = new List<k>(); ...

C# Defining the Length of a Generic created using Reflection

I have code like this: Type typPrecise = MostPrecise(typeof(int), typeof(double));//Evaluates to double var varGeneric = typeof(Number<>); var varSpecific = varGeneric.MakeGenericType(typPrecise); dynamic nmNumber = Activator.CreateInstance(varSpecific); The nmNumber is of dynamic type and essentially produces a Generic Number. How ...

C# Implementing IEquatable<T>.Equal<T>

I have a class like this: public class Foo<T> : IEquatable<T> where T : struct { List<T> lst; [Other irrelevant member stuff] } I want to implement the IEquatable<T> interface for the Foo class. What do I need to do. For simplicity I want to just check whether the List members are equal. Thanks. C# 4.0 supported answers ar...

How to set constrains on generic in Java?

Hi folks. First of all I'm only aware of Java basics. Now I have the following scenario: If have a generic class: public class ListObject<T> { // fields protected T _Value = null; // .. } Now I want to do something like the following: ListObject<MyClass> foo = new ListObject<MyClass>(); ListObject<MyClass> foo2 = new L...

Is it possible to deserialize JSON to List<MyObject<T,K>> with JSON.Net

I have a class: [Serializable] public class KVPair<TKey, TValue> { public TKey Key { get; set; } public TValue Value { get; set; } public KVPair(TKey k, TValue v) { Key = k; Value = v; } } that I create: List<KVPair<string,string>> kvPairs; Using the J...

C# Converting List<int> to List<double>

I have a List<int> and I want to convert it to a List<double>. Is there any way to do this other than just looping through the List<int> and adding to a new List<double> like so: List<int> lstInt = new List<int>(new int[] {1,2,3}); List<double> lstDouble = new List<double>(lstInt.Count);//Either Count or Length, I don't remember for (i...