generics

C#: Generic sorted container that can return the sorted position of a newly added object?

I need a generic container that keeps its elements sorted and can be asked where (at which position) it would insert a new element, without actually inserting it. Does such a container exist in the .NET libraries? The best illustration is an example (container sorts characters by ASCII value, let's assume unicode does not exist): sorte...

Designing a generic interface, require the type parameter on the type or method?

I have a very thin interface that's going to define one method. Should I require the type parameter on the class definition? public interface ISomethingFun<T> { void Do(T item); } or method definition? public interface ISomethingFun { void Do<T>(T item); } What rationale? Is one easier to implement, inherit, or generate dyn...

How does Java method dispatch work with Generics and abstract classes?

I ran into a situation today where Java was not invoking the method I expected -- Here is the minimal test case: (I'm sorry this seems contrived -- the 'real world' scenario is substantially more complex, and makes much more sense from a "why the hell would you do that?" standpoint.) I'm specifically interested in why this happens, I do...

How to use a generic type in generic method

EDITED to show real example How can I call a generic function from a generic type passed to a function? This seems like it should be intuitive, but I can't seem to get it to work. For example, can I call the cache.ResetCache() function in LocalDataObjectEngine below? The error I'm getting is 'Type T cannot be used as a parameter' pub...

Returning null value from generic method

So i have this method: internal K GetValue<T, K>(T source, string col) where T : IBaseObject { string table = GetObjectTableName(source.GetType()); DataTable dt = _mbx.Tables[table]; DataRow[] rows = dt.Select("ID = " + source.ID); if (rows.Length == 0) return K; return (K) rows[0][col]; ...

Including a generic class in Unity App.Config file

I have a class of type ISimpleCache<IBrokeredDataObject> that I want to add as a type alias (then a type) in the App.Config file the line <typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache<IBrokeredDataObject>, MyApplication" /> is obviously wrong due to the <>, however I'm not convinced escaping th...

Performance of Arrays vs. Lists

Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume processing. In general, one would opt for using Lists (List) due to their flexibility in size. On top of that, msdn documentation claims List...

Restricting T to string and int?

I have build myself a generic collection class which is defined like this. public class StatisticItemHits<T>{...} This class can be used with int and string values only. However this public class StatisticItemHits<T> where T : string, int {...} Won't compile. what am I doing wrong? ...

How to determine using reflection the generic parameter of the base class..

I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a static method and without instantiating a new MyClass instance how do I get the type of the generic parameter used to build the concrete base class? e.g in the above example System.Int32 ...

C# reflection: check if a class is derived from a generic class

I have a generic class in my project with derived classes. public class GenericClass <T> : GenericInterface<T> { ...... } public class Test : GenericClass <SomeType> { } Is there any way to find out if a Type object is derived from the GenericClass ? t.IsSubclassOf(typeof(GenericClass<>)) is not working. ...

How do you access Class object for generics?

How can I access Class object for Generics? Currently, I am doing it this way: List<String> list= new ArrayList<String>(); list.getClass(); Is this OK? Or, what should be the way? ...

How do I get the subclass Type object in a static superclass function in .net, using reflection?

Ok, so I'm trying to make a nice superclass for data-access objects that can generate a tsql query to search all of the subclass's public string properties. I want to use reflection to get the type of the subclass and then iterate through all of the public string properties on the object, since those property names are the same as the d...

Linq To Sql return from function as IQueryable<T>

Ok, I have managed to get the following working public IQueryable getTicketInformation(int ticketID) { var ticketDetails = from tickets in _context.tickets join file in _context.file_objects on tickets.ticket_id equals file.source_id where tickets.ticket_id == ticketID select new { tickets.ticket_id, tickets....

Java: How to check generic class type definitions?

The idea is to define a base class that can invoke methods defined in derrived classes, but at creation time I want to ensure, that such methods are defined exactly according to the requirements, which is that the methods take only one argument, a HashMap<String String>. So far I was able with the following code to check that the method...

Creating a new instance of n mostly unknown type

Hello! I'd like to write a generic method that creates a new instances of a specified Type. I tried protected T CreateNew<T>() where T : new() { return new T(); } This works, but only if I specified the type at compile time, just like var x = CreateNew<Point>(); The point is, that I need to do something like this ISomeInte...

How to generically specify a Serializable List

I have the following interface: public interface Result<T extends Serializable> extends Serializable{ T getResult(); } With that interface, I can not define a variable of type Result<List<Integer>> because List is not serializable. However, if I change the interface to this: public interface Result<T> extends Serializable{ ...

How to use Class<T> in Java?

There's a good discussion of Generics and what they really do behind the scenes over at http://stackoverflow.com/questions/31693/differences-in-generics so we all know that Vector < int[]> is a vector of integer arrays, and HashTable< String, Person> is a table of whose keys are strings and values Persons. What stumps me is the usage ...

Convert List<List<T>> into List<T> in C#

I have a List<List<int>>. I would like to convert it into a List<int> where each int is unique. I was wondering if anyone had an elegant solution to this using LINQ. I would like to be able to use the Union method but it creates a new List<> everytime. So I'd like to avoid doing something like this: List<int> allInts = new List<int>...

How to initialize a List<T> to a given size (as opposed to capacity)?

.NET offers a generic list container who's performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization. Arrays are very easy to initialize with a default value, and by definition they already have certain size: string[] Ar = new string[10]; which allows one to sa...

What is a good C# generics tutorial?

Does anyone know a really good C# generics tutorial? ...