generics

List<> of dynamicly created structure

In C# I want to create a list based on a dynamic value type, e.g.: void Function1() { TypeBuilder tb = .... // tb is a value type ... Type myType = tb.CreateType(); List<myType> myTable = new List<myType>(); } void Function2(Type myType) { List<myType> myTable = new List<myType>(); } This won't comple because List<> want...

Why should I care that Java doesn't have reified generics?

This came up as a question I asked in an interview recently as something the candidate wished to see added to the Java language. It's commonly-identified as a pain that Java doesn't have reified generics but, when pushed, the candidate couldn't actually tell me the sort of things that he could have achieved were they there. Obviously be...

How do you create a generic method in Java where one of the parameterized types must implement Iterable?

Here's the method I'm trying to write ( doesn't compile now, because what is not seen as an Iterable ): public <T,V> ArrayList<V> mySelect(T what,ITest<V> x) { ArrayList<V> results = new ArrayList<V>(); for(V value : what) { if(x.accept(value)) { results.add(value); } } return results; } ...

How do I write a generic Save() method that handles single objects and collections?

I have two generic save methods in a repository class: public void Save<T>(T entity) { _session.Save(entity); } public void Save<T>(IEnumerable<T> entities) { foreach (var item in entities) { _session.Save(item); } } However, when I use Save(collection) (which infers the type automatically), it recognizes it a...

Difference between SomeClass<T extends OtherClass> and SomeClass<T super OtherClass>

The title says it all really. What's the difference between: SomeClass<T extends OtherClass> and SomeClass<T super OtherClass> Thanks ...

How to return a generic list collection in C#?

Hi I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do this public List<T> thisIsAtest() { return query; } but I get this error Error...

Java Casting Problem

Why isn't a Map<String,List<SomeBean>> castable to Map<String,List<?>>? What I'm doing now is this: Map<String, List<SomeBean>> fromMap = new LinkedHashMap<String, List<SomeBean>>(); /* filling in data to fromMap here */ Map<String,List<?>> toMap = new LinkedHashMap<String, List<?>>(); for (String key : fromMap.keySet()) { toMap....

Detect generic parameters of MethodBase

This question is continue of How to distinguish MethodBase in generics In brief: I need to distinguish in Dictionary same generic method when it is called for different generic types substitution. static Dictionary<MethodBase, string> cache = new Dictionary<MethodBase, string>(); static void Method1<T>(T g) { MethodBase m1 = Meth...

Performance of dynamically created type

Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects. Each method performs differently: Button1: 15 milliseconds Button2: 31 milliseconds Button...

Get generic type of java.util.List

I have; List<String> stringList = new ArrayList<String>(); List<Integer> integerList = new ArrayList<Integer>(); Is there a (easy) way to retrieve the generic type of the list? ...

"Elegant" way to get list of list of property values from List<T> of objects?

Say I have a Customer class with the usual properties: CustomerID, Name, etc. As a result of a query, I get a generic list of Customer objects: List<Customer> Is there an elegant way to get an array/list of CustomerID or Name property values from this generic list? (i.e. string[] customerIDs = ???? ) I know I could do a foreach and fi...

generics multiple types

Defining a method as myMethod(Object... obj){} allows arbitrary number and types of parameters to be used. I'd love to use generics for strict definition of the number and types of parameters. For example, Let's assume that the above mentioned myMethod(Object...) is a method in a class named MyClass and MyClass can be instantiated b...

Why can't we use sealed classes as generic constraints?

Can you guess what is the reason to not allow sealed classes for type-constraints in generics? I only have one explanation is to give opportunity to use naked constraints. Sorry, for editing. I think you need to rephrase the question title. It should be "Why we can’t use sealed classes as generic constraints?" – this.__curious...

IList<T> where T is a generic class

Hello there.. I need to create a generic IList where T would be a generic class with various interfaces. For example ChannelFactory<IService1> or ChannelFactory<IService2> and etc .. ...

Error when using constructor for a generic list that takes a collection

I was trying out parsing XML using Fluent interface as described here. The example is good but gives one compiler error that I am not able to fix. The error is in the protected constructor below: protected DynamicXml( IEnumerable<XElement> elements ) { _elements = new List<XElement>( elements ); // error on this line } The two c...

Java Generics GetThis Trick Explanation

I am reading about Java Generics and I came across this topic where I am a bit confused. From : http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205 public abstract class Node <N extends Node<N>> { private final List<N> children = new ArrayList<N>(); private final N parent; protected Node(N par...

How to compare generic types?

I have a class which has some properties of type List<float>, List<int> etc. Now I am quering the properties of this class through reflection so that I get a list of PropertyInfo. I want to filter the types which are of type List<>. But the comparison propertyInfo.PropertyType == typeof(List<>) fails. I can get around this by compar...

Java generic function: how to return Generic type

Here's a Java generic pattern: public <T> T getResultData(Class<T> resultClass, other_args) { ... return resultClass.cast(T-thing); } A typical call looks like: DoubleBuffer buffer; buffer = thing.getResultData(DoubleBuffer.class, args); I've never been able to figure out how to use this pattern cleanly when the desire...

How to find object of given type in a heterogeneous List

I have a heterogeneous List that can contain any arbitrary type of object. I have a need to find an element of the List that is of a certain type. Looking through the answers of other generics related questions, I'm not finding exactly what I need. Here's an example of what I'm trying to accomplish: List <Object> list = new ArrayList ...

What is the C++/CLI equivalent to C#'s default(T)?

I'm working with some C++/CLI code (new syntax) and am trying to declare a generic type and want to set a member variable to it's default. In C#: class Class<T> { T member = default(T); } What's the equivalent in CLI? generic<typename T> public ref class Class { public: Class() : member(default(T)) // <-- no worky { ...