generics

.Net Collections: Node-Leaf

Is there a built in node-like collection that will allow me to store information within information without building long hard to read generic declarations? C# or VB.Net solutions would be appreciated. For example: dim base as new Dictionary(of String, Dictionary(of String, List(of String))) dim mid as new Dictionary(of String, List(o...

Getting class of return generic type possible?

I want to know the class of a generic return type of a method, something like this: <T> T getEntry() { System.out.println(T.class) } The problem is, this is not a generic class, this is just a generic method, so I can't extract the generic type from the class. What I want to achieve is knowing what concrete type the caller wants w...

Generics in for each loop problem if instance does not have generic type assigned

Hi, Could someone please explain to me why there is explicit need to assign generic type for ForEachLoop instance? Why compiler complains: Type mismatch: cannot convert from element type Object to String? JDK 1.5.0_09 import java.util.ArrayList; import java.util.Collection; public class ForEachLoop<T> { public static void main(St...

Static factory method with generics

I have a class EnumConverter<T extends Enum<?>> that converts strings to the correct enum constant for the enum T (I can't use Enum.valueOf). I construct instances of this class in a factory method like this: public static <T extends Enum<?>> EnumConverter<T> getInstance(Class<T> enumClass) { return new EnumConverter<T>(enumClass.g...

2 methods using 2 generics of same type

If I have a class as below: class MyClass<T,U> { public void DoSomething(T t) { } public void DoSomething(U u) { } } But construct it using the same types (new MyClass<int,int>()) This compiles fine, but if I try to call DoSomething it errors because of an ambiguous call, which of course is correct. But what if the method...

How can you create a collection based on multiple IEnumerables

A class I want to operate on provides getters of type IEnumerable<X> and IEnumerable<Y> where X & Y are subclasses of base type T. I want to iterate over the contents of both treating them as type T. Is there a handy way to concatenate both into something which can be seen as IEnumerable<T>? Example: IEnumerable<HeaderPart> hea...

add generic Action<T> delegates to a list

Is it possible to add a generic delegate Action to a List collection? I need some kind of simple messaging system for a Silverlight application. UPDATE The following is what i realy "want" class SomeClass<T> { public T Data { get; set; } // and more .... } class App { List<Action<SomeClass<T>>> _actions = new List<Action<S...

Why can I not pass this interface as an argument?

I don't have much experience with generics, but can someone please explain to me why this doesn't work and what I need to do to get it to work? I have 3 interfaces. public interface IOnlineView public interface ICalendarView : IOnlineView public interface IDateView : ICalendarView Then I have 3 presenter classes public abstract cl...

How to compare list of X to list of Y in C# by using generics?

I have 2 classes, X and Y. Both classes have same similar property like below. class X { public string T1 { get; set; } public string T2 { get; set; } public string T3 { get; set; } } class Y { public string T1 { get; set; } public string T2 { get; set; } public string T3 { get; set; } public string O1 { ge...

What's Wrong with an ArrayList?

Recently I asked a question on SO that had mentioned the possible use of an c# ArrayList for a solution. A comment was made that using an arraylist is bad. I would like to more about this. I have never heard this statement before about arraylists. could sombody bring me up to speed on the possible performance problems with using arrayli...

How come generic type parameter says "extends" Comparable not "implements" ?

I tried to write generic function that remove the duplicate elements from array. public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) { //do quicksort Arrays.sort(arr); ArrayList<E> list = new ArrayList<E>(); int i; for(i=0; i<arr.length-1; i++) { if(arr[i].co...

.NET Micro framework and unsupported features. What is the impact?

Hi, I found this table listing the limitations of the .NET Micro framework in embedded development, it states that generics are not available due to the size of the image this would create. The memory footprint needs to be below 300KB, and the inclusion of generics pushes the size over this limit. Does this mean that any Micro framewor...

Generic method: instantiate a generic type with an argument

I have a generic method that takes in a type T, which i need to be able to call a constructor on that requires a single XmlNode. Currently, I am trying to do that by having an abstract base class that has the constructors I want (plus a parameterless one so i don't have to edit the "subclasses" other than to add the actual subclassing) a...

Specialize a generic variable

I have a generic method where I want to do something special for Strings. I've found DirectCast(DirectCast(value, Object), String) to get the String value (when I've already confirmed GetType(T) Is GetType(String)) and DirectCast(DirectCast(newvalue, Object), T) as mentioned in a number of answers to similar questions works. But is the...

Scala class cant override compare method from Java Interface which extends java.util.comparator

Hello All I'm currently working on a port of a jEdit plugin to write all code in Scala. However Im forced at a certain point to implement my own Comparator. My simplified code is as follows: class compare extends MiscUtilities.Compare { def compare(obj1: AnyRef, obj2: AnyRef): Int = 1 } The MiscUtilities.Compare has the following ...

Generic constraint to match numeric types

I'm trying to write an extension method on numeric types to be used in a fluent testing framework I'm building. Basically, I want to do this: public static ShouldBeGreaterThan<T>(this T actual, T expected, string message) where T : int || T: double || etc... Just where T : struct doesn't do, since that will also match string and b...

Is this an example of wrong usage of generics?

Hi, I wrote this method: public IGamePlugin[,] GetTable<T>() { Type t = typeof(T); if (t is IFixedElement) { return fixedElements; } else if (t is IFixedTile) { return fixedTiles; } else { throw new NotSupportedExceptio...

Abstract generic classes taking type parameters that are themselves derived from that class

Do you consider it an acceptable or a bad practice to create an abstract generic class that takes as a type parameter a class that derives from itself? This allows the abstract generic class to manipulate instances of the derived class and in particular the ability to create new() instances of the derived class as necessary and can he...

Generic Proxy Class

This may not the worded quite right but here goes anyway. If I have the following statements; DocumentMerger<EmailMerge> mailMerge = new DocumentMerger<EmailMerge>(); List<MailMessage> mailMessages = mailMerge.Process(); DocumentMerger<SmsMerge> mailMerge = new DocumentMerger<SmsMerge>(); List<SmsMessage> smsMessages = mailMerge.Proce...

Return a different list depending on generic

I've asked this question before but I don't think I explained myself clearly so I'm going to try again. I have the below code; public interface IDocumentMerger { List<???????> Process(); } public class EmailMerger : IDocumentMerger { public void Process() { return new List<MailMessage>(); } } public class Doc...