generics

C# Templates and special constructors

I have a set of some classes which are all capable of being constructored with an argument being an instance of a particular interface. Since they all can be constructed by this same object (and the process during which this construction happens is largely the same in all cases), I figured perhaps templating would work. Basically, I wa...

Howto cast pointer to generic parameter type?

Hello everybody, it's my first question here, glad to have found this site. My question deals with the new Generics feature in Delphi 2009. Basically I tried to write a generic wrapper class for an existing hash map implementation. The existing implementation stores (String, Pointer) pairs, so in the wrapper class I have to cast between...

Dynamically creating a new instance of IList's type

Hi, My application is processing IList's. ILists of different user defined types. I'm thinking that i can use reflection to to see what type of object the IList contains and then create a new instance of that type and subsequently add that to the IList itself? So at any one time I might be processing IList<Customer> l; and I'd like ...

Generic types not equal

The code segment below prints out "The types ARE NOT the same.". Why? I am aware of the fact that using interfaceOnMyType.GetGenericTypeDefinition() will solve the problem, but why should I have to do that? class Program { static void Main(string[] args) { var myType = typeof(Baz<>); var interfaceOnMyType = myType.Ge...

Delphi 2009: Is it possibly dangerous to use TList<[any interface]> with default comparator?

Hello everyone, I am wondering if the usage of a generic TList<T> where T is any interface type (except IUnknown/IInterface) might be dangerous. I am heavily using interfaces and store them in lists. Some interfaces are my own, some are provided by some COM-interfaces, so COM is involved. I see a potential problem where checks for inst...

How to handle this c# generics problem ?

I have a base class that inherits List<> which has multiple classes derived from it. I want to create a method that can add items to any class that has inherited from this base class, is this possible ? pseudo code: public class BaseList<T> : List<T> where T: baseItem { } public BaseList<T> AddItems<T>(int someArg) where T: new(), ba...

How do you return more than one DataTable from a method?

I have class which has a method that needs to return three DataTables. I thought I could use Generics but honestly I've never used them, so I'm trying figure it out. It may not be the right thing here. I have in my class Employee: public List<Employee> GetEmployees() { //calls to other methods in my class; //psuedocode GetDataT...

Overloading methods in C# .NET

A variable of the type Int32 won't be threated as Int32 if we cast it to "Object" before passing to the overloaded methods below: public static void MethodName(int a) { Console.WriteLine("int"); } public static void MethodName(object a) { Console.ReadLine(); } To handle it as an Int32 even if it is cast to "Object" can be achieved ...

Anonymous Delegates and generic Lists in C#

Can you explain me code below : private static List<Post> _Posts; public static Post GetPost(Guid id) { return _Posts.Find(delegate(Post p) { return p.Id == id; }); } What is the point to find an object in a generic list by that way ? He can simply iterate the list. How this delegated method called for each element ...

C# specifying generic collection type param at runtime

I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do: Type t = typeof(Car); List<t> Cars = GetAll<t>(); How can I do this? I want to return a generic collection from the database of a type that I discover at runtime using reflection. ...

limit map key and value types - more complicated

I have a more complicated issue (than question 'Java map with values limited by key's type parameter' question) for mapping key and value type in a Map. Here it is: interface AnnotatedFieldValidator<A extends Annotation> { void validate(Field f, A annotation, Object target); Class<A> getSupportedAnnotationClass(); } Now, I want to...

How do I declare the constructor for a generic class with a non-generic base class with parameters

I have a base class which is non-generic with a derived generic class. The AbstractRepository and ILoggingManager are provided by the IoC framework. Base Class public abstract class EventHandlerBase : AbstractDataProvider , IEventHandler { public EventHandlerBase( AbstractRepository data, ILoggingManager log...

Why do I have to specify all of the generic type parameters?

Is there any (technical) reason that C# requires all generic type parameters to be declared with their enclosing class' names? For example, I'd like to declare this: public class FruitCollection<TFruit> : FoodCollection<TFoodGroup> where TFruit : IFood<TFoodGroup> where TFoodGroup : IFoodGroup { } public class AppleCollection ...

Java: Array of primitive data types does not autobox

I have a method like this: public static <T> boolean isMemberOf(T item, T[] set) { for (T t : set) { if (t.equals(item)) { return true; } } return false; } Now I try to call this method using a char for T: char ch = 'a'; char[] chars = new char[] { 'a', 'b', 'c' }; boolean member = isMemberOf(ch, ...

Why do some claim that Java's implementation of generics is bad?

I've occasionally heard that with generics, Java didn't get it right. (nearest reference, here) Pardon my inexperience, but what would have made them better? ...

Creating a generic based on class Type

If I had generic class: public class GenericTest<T> : IGenericTest {...} and I had an instance of Type, which I got through reflection, how could I instantiate GenericType with that Type? For example: public IGenericTest CreateGenericTestFromType(Type tClass) { return (IGenericTest)(new GenericTest<tClass>()); } Of course, the ...

Code Generators or T4 Templates, are they really evil?

I have heard people state that Code Generators and T4 templates should not be used. The logic behind that is that if you are generating code with a generator then there is a better more efficient way to build the code through generics and templating. While I slightly agree with this statement above, I have not really found effective wa...

Cannot implicitly convert List<T> to Collection<T>

This is a compiler error (slightly changed for readability). This one always puzzled me. FxCop tells that this is a bad thing to return List and classes that are\derived from Collection<T> should be preferrable as return types. Also, FxCop says that it is OK to use List<T> for internal data storage\manipulation. Ok, I get it, but what ...

Can a generic class be forced to have a type inherit from one of two interfaces?

I have a generic class, but I want my type to be forced to inherit from either one or the other interface. For example: public class MyGeneric<T> where T : IInterface1, IInterface2 {} The above will force T to inherti from both IInterface1 and IInterface2 but can I force T to inhert from IInterface1 OR IInterface2 (or both)? ...

XML Documentation: <see> tag with multiple generic type parameters

I'm trying to use XML documentation on the members of a class, and in a certain case I would like to reference a generic type using a tag. The problem here is that a warning is generated when multiple generic type parameters are specified. It was easy enough to find how to reference a generic type with a single parameter, as such: <see...