generics

Java using generics on Map with two types

I have two methods, the first needs a Map<ItemA, ItemB> the second a Map<ItemA ,ItemB[]>. How can I create a generic Map that covers both cases? ...

What CLR do when compare T with null, and T is a struct?

private static void SaveOrRemove<T>(string key, T value) { if (value == null) { Console.WriteLine("Remove: " + key); } //... } If I call passing 0 to value: SaveOrRemove("MyKey", 0), the condition value == null is false, then CLR dont make a value == default(T). What really happens? ...

How can I recognize a generic class?

How can I recognize (.NET 2) a generic class? Class A(Of T) End Class ' not work ' If TypeOf myObject Is A Then ? ...

method implementation for this case in Java

Hello, I just saw a code snippet like this: private static class DefaultErrorHandler<RT> implements ErrorHandler<RT> { public RT handle(Object[] params, Throwable e) { return Exceptions.throwUncheckedException(e); } Now I am wondering what the static method "throwUncheckedException (Throwable e)" would return exactly and how it...

Sorting Winforms TreeView in a type safe way?

Is it possible to sort a TreeView collection using IComparer<T> and not IComparer? After all they are all TreeNode values, right? ...

List of Lists of different types

One of the data structures in my current project requires that I store lists of various types (String, int, float, etc.). I need to be able to dynamically store any number of lists without knowing what types they'll be. I tried storing each list as an object, but I ran into problems trying to cast back into the appropriate type (it kep...

C# Fun with Generics - Mutual Dependencies

As an experiment I'm trying to write a generic MVP framework. I started with: public interface IPresenter<TView> where TView: IView<IPresenter<... { TView View { get; set;} } public interface IView<TPresenter> where TPresenter:IPresenter<IView<... { TPresenter Presenter { get; set; } } Obviously this can't work because the t...

Suggestions on Working with this Inherited Generic Method

We have inherited a project that is a wrapper around a section of the core business model. There is one method that takes a generic, finds items matching that type from a member and then returns a list of that type. public List<T> GetFoos<T>() { List<IFoo> matches = Foos.FindAll( f => f.GetType() == typeof(T) ); L...

Maximum number of type parameters used in a java class

What is the maximum number of type parameters you used in a java class? Example for 4: class MyClass<A, B, C, D> { } ...

Invoking EventHandler generic, TargetParameterCountException

Hi, I have a DirectoryMonitor class which works on another thread. It has the following events declared: public class DirectoryMonitor { public event EventHandler<MonitorEventArgs> CreatedNewBook; public event EventHandler ScanStarted; .... } public class MonitorEventArgs : EventArgs { public Book Book { get; set; } } ...

How to Work Around Limitations in Generic Type Constraints in C#?

Okay I'm looking for some input, I'm pretty sure this is not currently supported in .NET 3.5 but here goes. I want to require a generic type passed into my class to have a constructor like this: new(IDictionary<string,object>) so the class would look like this public MyClass<T> where T : new(IDictionary<string,object>) { T Creat...

Is generic Money<TAmount> a good implementation idea?

I have a Money Type that allows math operations and is sensitive to exchange rates so it will reduce one currency to another if rate is available to calculate in a given currency, rounds by various methods. It has other features that are sensitive to money, but I need to ask if the basic data type used should be made generic in nature. ...

C# - implementing GetEnumerator() for a collection inherited from List<string>

Hi, I am trying to implement FilePathCollection. Its items would be simple file names (without a path - such as "image.jpg"). Once the collection is used via foreach cycle, it should return the full path created by concatenating with "baseDirectory". How can I do that? public class FilePathCollection : List<string> { string baseDi...

Generic collection as a Java method argument

Is there any way to make this work in Java? public static void change(List<? extends Object> list, int pos1, int pos2) { Object obj = list.get(pos1); list.set(pos1, list.get(pos2)); list.set(pos2, obj); } The only way I've successfully avoided warnings and errors is this: public static <T> T change(List<T> list, int pos1, in...

What does lacking of generics mean in programming?

In the comments to this answer someone says I have to downvote recommending Apache Commons in general since it's really a 50:50 chance on if you find something useful or not. There's certainly a lot of gems in there but there's also lots of bad and outdated stuff too, for example the worrying lack of Generics at this point is just i...

Generic arrays of parametrized ArrayLists in java?

I am new to Java, so I am not aware of the nitty gritties. Why can't I create generic array of parametrized ArrayList? Instead I have to write, ArrayList<String>[] alist = new ArrayList[10]; or I have to create List of ArrayLists. Aren't arrays supposed to be more efficient than ArrayLists? Then why doesn't Java allow it? Also, wha...

c# Generic List

I m populating data for different entities into set of lists using generic lists as follows : List<Foo> foos .. List<Bar> bars .. I need to write these lists to a file, i do have a util method to get the values of properties etc. using reflection. What i want to do is: using a single method to write these into files such as: void w...

CreateDelegate with unknown types

Hello, I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main<T> and a method which looks like this: Delegate.CreateDelegate(typeof(Func<T, object>), get) where get is a MethodInfo of the property that should be read. The problem is that when the property return...

Scala type inference failure on "? extends" in Java code

I have the following simple Java code: package testj; import java.util.*; public class Query<T> { private static List<Object> l = Arrays.<Object>asList(1, "Hello", 3.0); private final Class<? extends T> clazz; public static Query<Object> newQuery() { return new Query<Object>(Object.class); } public Query(Class<? ext...

Java recursive generics class wildcard matching

Having these generic interface and class: interface TestIntf<T extends TestIntf<T>> { void test(T param); } class TestImpl<T extends TestIntf<T>> implements TestIntf<T> { @Override public void test(T param) { System.out.println(param); } } This fails: Class<? extends TestIntf<?>> clz = TestImpl.class; (Type mismatch:...