generics

Put together objects with different generic types

Given a generic type TMyClass <T> = class ... end; is there a way to put multiple instances (instantiated with different types) together like FList : TList <TMyClass>; FList.Add (TMyClass <Integer>.Create); FList.Add (TMyClass <String>.Create); or FArray : array [0..1] of TMyClass; FArray [0] := TMyClass <Integer>.Create; FArray...

Java Raw Type and generics interaction

If I have a Stack class class Stack<E> {} now if I do: 1) Stack<Integer> s = new Stack() 2) Stack s = new Stack<Integer>() 3) Stack s = new Stack() can anyone explain me what these interactions (generic<->raw) causes? Mainly my doubt is on point 1. In fact if I do that the assignment it's unsafe because that stack can store type...

C# generics compared to C++ templates.

Possible Duplicate: What are the differences between Generics in C# and Java and Templates in C++? What are the differences between C# generics compared to C++ templates? I understand that they do not solve exactly the same problem, so what are the pros and cons of both? ...

An array or Generic collection?

I need to pass a list of Days (number and name) to an view! Whats the best way to do this? I was thinking of creating a generic collection, but not sure how? or an array? ...

Will there be generic attributes in C# 4?

So - if there isn't particular reason why there isn't generic attributes, I'm wondering - maybe they will be implemented? Those would be great for ASP.NET MVC action filters. ...

How to pass a type to a method - Type argument vs generics.

I have a method of an object which is something like a factory. You give it a type, it creates an instance and does a few other things. An elegant way to do it (in my opinion) is like this: public T MagicMethod<T>() where T: SomeBaseClass { // Magic goes here } But this upsets FxCop who says that this is a bad style - I get a "CA1...

XML Serialize generic list of serializable objects

Can I serialize a generic list of serializable objects without having to specify their type. Something like the intention behind the broken code below: List<ISerializable> serializableList = new List<ISerializable>(); XmlSerializer xmlSerializer = new XmlSerializer(serializableList.GetType()); serializableList.Add((ISerializable)Pers...

Defining Typed Parameters as the Ones that Define Operator Overloading

Is there anyway to define type parameter T as the ones that define operator overloading? Take for example, I need to create a generic Add function with the parameter T. Obviously, the T must defined the + operator, i.e., public static T operator +(T c1,T c2) { // plus operation } Is there anyway to constraint T so that it is restri...

How to find the parameterized type of the return type through inspection?

I am using reflection to get all the get all the methods in a class like this: Method[] allMethods = c.getDeclaredMethods(); After that I am iterating through the methods for (Method m: allMethods){ //I want to find out if the return is is a parameterized type or not m.getReturnType(); } For example: if I have a method like...

C# Type Conversion Error Despite Generic Constraint

Why, with a generic constraint on type parameter T of class P of "must inherit from A", does the first call succeed but the second call fail with the type conversion error detailed in the comment: abstract class A { } static class S { public static void DoFirst(A argument) { } public static void DoSecond(ICollection<A> argument...

How to use C# generics in place of string and byte[]

Is it possible to use C# generics replace these 4 routines with one? int memcmp (string a, string b){...} int memcmp (string a, byte[] b){...} int memcmp (byte[]a, string b){...} int memcmp (byte[]a, byte[] b){...} I've tried a number of variations, but cannot determine exactly what to use... For example... int memcmp<A, B>( A a, ...

IList<IWhatever> as a method parameter

I have two IList<ICat> and I'm trying to create a method which takes an IList<ICat> and does some work. I'm having problems trying to pass either an IList<PussyCat> or IList<OtherCat> to it, both PussyCat and OtherCat implement ICat. I've tried: List<PussyCat> cats = ... DoWork((IList<ICat>)cats); and just DoWork(cats); But neithe...

C# - Generic CRUD operation

How to achieve generic Save and Update operation using generics and reflection in C#? I have achieved data retrieve using some reference from here... I don't have much time to learn technologies like NHibernate/LINQ/Entity Frameowrk, etc. for this moment. I need a quick solution for this problem for some reason. ...

Question on C# Generic Collection

In the following code, the Type of our Dictionary is <int, Customer>, but how do I know what's the type of Customer? It seems like Customer is a string here since we're Customer cust1 = new Customer(1, "Cust 1"); .... im confused... public class Customer { public Customer(int id, string name) { ID = id; ...

When should or shouldn't I be using generic type constraints?

I've got a base class: public abstract class StuffBase { public abstract void DoSomething(); } And two derived classes public class Stuff1 : StuffBase { public void DoSomething() { Console.WriteLine("Stuff 1 did something cool!"); } public Stuff1() { Console.WriteLine("New stuff 1 reporting for...

How can I retrieve all the KeyValuePairs contained in a Dictonary<?,?> ?

I've got an object which is a Dictionary of an unknown type (ie I don't know the type for the key and the value) I want to retrieve all of its values so I can access those by index. So what I want to do is something like that : Dictionary<object, object> d = (Dictionary<object, object>)obj; // cast error l = new List<KeyValuePair<obje...

IList<variableType>; Solve without Generics

I've found many similar questions but nothing with quite the answer I'm after. Part of my problem is due to the inability to use Generics in Attributes. I am probably trying to over-complicate things, so if you can think of an easier way of doing this I'm all ears. My specific problem relates to ASP.NET MVC, using Attributes (Filters) ...

Using generics to process asp.net form request variables into business object

Using jquery to post values back to an asp.net webform. I have some code that works great for sticking posted Form values into my object, basically it converts the form values into the correct type for each property in my class. if (Request.Form.HasKeys()) { foreach (var key in Request.Form.AllKeys) { PropertyInfo itemP...

Generics ListView Custom Control

Good Day, I'm using Generics with a ListView control whose initial class definition looks like this: namespace BaseControlLibrary { public partial class CustomListView<T> : System.Windows.Forms.ListView { // Custom fields, properties, methods go here public CustomListView(List<T> data) { _co...

Getting Allen Bauer's TMulticastEvent<T> working

Hi all! First Question! :) I've been mucking around with Allen Bauer's code for a generic multicast event dispatcher (see his blog posts about it here). He gives just enough code to make me want to use it, and unfortunately he hasn't posted the full source. I had a bash at getting it to work, but my assembler skills are non-existent. ...