generics

How do I get the user defined object type given the class name?

If I have the class name of the user defined object as a string how do I use it in a generic function as Type of the object ? SomeGenericFunction(objectID); ...

How to Implement Generic Method to do Math calculations on different value types

I have a method that accepts an IEnumerable-decimals and performance various math functions. I want to use the same method on an IEnumerable-int-. How do I implement this? For example to find a simple sum? void Calculate<T>(IEnumerable <T> ListOFNumbers) { int count= ListofNumbers.Count(); ?sum=?; } ...

Java Generics Help

I'm working on this homework that's kind of confusing me... I am provided with the following BinarySearchTree class import java.util.NoSuchElementException; /** * * @param <T> The type of data stored in the nodes of the tree, must implement Comparable<T> with the compareTo method. */ public class BinarySearchTree<T extends Compara...

Creating an array problem. help please

Dupe: http://stackoverflow.com/questions/185594/java-generics-syntax-for-arrays I want to create an array that will hold linked lists of Integer type.. import java.util.LinkedList; public class Test { public static void main(String [] args){ LinkedList<Integer> [] buckets = new LinkedList<Integer>[10]; } } I ge...

How do I resolve ambiguous methods caused by intersection types in Java generics?

I just recently discovered that you can specify multiple types in a single type parameter bound (see example). Like any new tool, I've been trying to explore the possibilities of how this can be used (and misused). I crafted this example to help illustrate. On the sample below, the compiler is giving me an error dispatch(new Alphab...

Why it is not possible to create a generic fill method in Java?

I have the following classes: abstract class DTO{ } class SubscriptionDTO extends DTO { } and the following generic method: protected void fillList(ResultSet rs, ArrayList<? extends DTO> l) throws BusinessLayerException { SubscriptionDTO bs; try { while (rs.next()){ //initialize bs object... l.add(bs); //compiler error h...

generic interface: list of something specific

I want to define an interface MyList which is a list of interface MyThing. Part of the semantics of MyList is that its operations don't have any meaning on objects which do not implement the MyThing interface. Is this the right declaration? interface MyList<E extends MyThing> extends List<E> { ... } edit: (part 2) Now I have another...

C# syntax for declaring a variable of an abstract generic type

I have a class defined as follows; public abstract class Repository<TEntity, TDataContext> : DisposableBaseClass where TEntity : class where TDataContext : DataContext, new() {...contains Linq to SQL related functionality In the concrete sub-class I define the types as so; public class ConcreteRepo : Repository<LSTableClass, LS...

C#, List<T>.Contains() - too slow?

Could anyone explain me why the generics list's Contains() function is so slow? I have a List with about a million numbers, and the code that is constantly checking if there's a specific number within these numbers. I tried doing the same thing using Dictionary and the ContainsKey() function, and it was about 10-20 times faster than with...

JAXB Marshalling and Generics

I am trying to use JAXB's introspection to marshall and unmashall some existing domain objects marked up with JAXB annotations. Most things work as expected, but I am having quite a bit of trouble getting a fairly simple class to serialize. This class is used as an @XmlElement on a number of beans and looks something like: public class ...

generic class and generic methods

class test <T> where T : class { public void Write<T>() { Console.Write(typeof(T).FullName); } } In the above class, it is possible to pass in a string for the class (test<string> Test = new test<string>) and then int for the method? If so, what is the output? If not, what problems does this cause? I haven't actuall...

Does the C# 4.0 "dynamic" keyword make Generics redundant?

hey guys, I'm very excited about the dynamic features in C# (C#4 dynamic keyword - why not?), especially because in certain Library parts of my code I use a lot of reflection. My question is twofold: 1. does "dynamic" replace Generics, as in the case below? Generics method: public static void Do_Something_If_Object_Not_Null<SomeType...

How to refactor these 2 similar methods into one?

Hello, I've seen some samples of using 'T' to make a method reuseable for generic collections of different classes, but I've never really gotten into it or understood the samples. I wonder if it would be possible to put the 2 methods below into one and what the downsides of doing this would be (performance-wise). Anyone? ...

C#: Is it possible to return an IOrderedEnumerable<T>?

Is it possible to return an IOrderedEnumerable<T> from a method without using the OrderBy or OrderByDescending methods on an IEnumerable<T>? Im guessing perhaps not... but... maybe I am wrong? Reason: Mostly curiosity. It just kind of hit me when making this answer on returning digits in a number. And my method would return the digit...

asp.net - passing generic lists

I have a utility class that takes a generic list as a parameter. Code looks like: Function DoStuff(collection as Object, elt as Object) ... collection.Add(elt) ... End Function This is called with: DoStuff( List(Of Foo), new Foo() ) DoStuff( List(Of Bar), new Bar() ) There are about a dozen different types. Currently, pa...

C# generics list of objects used as a property - can't add values

I'm trying generics for the first time and am having a problem. I have a dll that sends messages in batches there is a "Message" class and a "Batch" class in that dll on the batch class, I have some public properties on of the batch class's public properties is a property called "Messages" which is a list of the "Message" class as fol...

Why is this cast not possible?

the title says it all ... interface IFolderOrItem<TFolderOrItem> where TFolderOrItem : FolderOrItem {} abstract class FolderOrItem {} class Folder : FolderOrItem {} abstract class Item : FolderOrItem {} class Document : Item {} now i'm trying to do sth like this: class Something { IFolderItemOrItem<Item> SelectedItem { get; s...

Generic method with inferred generic arguments

I have the following code to create an expression of a func for accessing a property of a class public static Expression<Func<TObj, TProperty>> BuildGet<TObj, TProperty>(PropertyInfo property) { Type type = typeof(TObj); ParameterExpression arg = Expression.Parameter(type, "x"); var prop = Expression.Property(arg, property);...

Why does nullable KeyValuePair<,> have no key property?

I have the following: KeyValuePair<string, string>? myKVP; // code that may conditionally do something with it string keyString = myKVP.Key; // throws 'System.Nullable<System.Collections.Generic.KeyValuePair<string,string>>' // does not contain a definition for 'Key' I'm sure there is some reason for this as I can see that the type...

array of stacks

Is it possible to create an array of stacks without having to cast the stacks as they come out? Eclipse gives me a warning about not being able to make a generic array of Stack when I do something like this: Stack<Card>[] cards = new Stack<Card>[52]; ...