generics

How can I work around C#'s limitation on calling static functions on a Generic type

I have the following extension method, and would like to make it more generic so I don't have to implement it for every class in our domain. public static IList<User> ToList(this DataTable table) { IList<User> users = new List<User>(); foreach (DataRow row in table.Rows) users.Add(User.FromDataRow(row)); return use...

Interface exposes type A, but implementation requires type B (subclass of A)

I have a system which I've been wrestling with for a while. Essentially, it uses a lot of abstraction to deal with the fact that later extension is not just expected, but necessary. One place this is required is data access. The system generally deals with managing objects encapsulating some observation (in the sense of an observed value...

C#: 'default' keyword with Generics

I have written the following method to return a list of Unserializable classes (LINQ classes) from a list of Serializable classes (POCOs): List<UnSerializableEntity> ToListOfUnserializables(List<SerializableEntity> entityList) { var tempList = new List<UnSerializableEntity>(); entityList.ForEach(e => { if (e != null)...

How can I work around the __Canon type problem in generic exception handlers?

Given an extension method like this: Public Sub RehydrateTo(Of T As New)(ByVal input As String, ByRef output As T) Dim ms As MemoryStream = MsFromString(input) Dim f As New DataContractSerializer(GetType(T)) Try output = CType(f.ReadObject(ms), T) Catch ex As SerializationException output = New T Dim ild ...

Is there a better way of filling a generic List from a SQL DataAdapter?

I have an existing application that uses Active Record for its data retrieval. It's in VB.NET (first time I'm doing VB.NET; I usually work in C#). And I'm building a method to return a List(Of T) of an object. The current pattern uses a SQLDataAdapter to populate a datatable. I COULD add the record to the List(Of T) as I fill the datat...

C# generics vs C++ templates - need a clarification about constraints

Duplicate What are the differences between Generics in C# and Java… and Templates in C++? Hi all, I am experienced C++ programmer but quite new to C#. Whats up with those constraints and generics? Why doesn't it work the same as in C++ where constraints are implicit and derived from the instantiations you do to the template cla...

Returning proper type after using OrderBy()

I have a collection class that inherits from List<>. I've set up a function to sort the list by a certain property like so: public PlaylistCollection SortByName(IEnumerable<Playlist> playlists) { return (PlaylistCollection)playlists.OrderBy(p => p.Name); } When I try to use the sorted results in my code like this: artistSource.Pl...

Java Generics Wildcarding With Multiple Classes

I want to have a Class object, but I want to force whatever class it represents to extend class A and implement interface B. I can do: Class<? extends ClassA> Or: Class<? extends InterfaceB> but I can't do both. Is there a way to do this? ...

Java Generic List<List<? extends Number>>

How come in java we cannot do: List<List<? extends Number>> aList = new ArrayList<List<Number>>(); Even though this is OK: List<? extends Number> aList = new ArrayList<Number>(); Compiler error message is: Type mismatch: cannot convert from ArrayList<List<Number>> to List<List<? extends Number>> ...

Difference between JVM implementations

Where do JVM Implementations differ (except licensing)? Does every JVM implement Type Erasure for the Generic handling? Where are the differences between: JRockit IBM JVM SUN JVM Open JDK Blackdown Kaffe ..... Deals one of them with Tail-Call-Optimization? ...

IList<IClient> method<T>() where T : Iclient can not add client object to list

Hi all, public IList<T> GetClientsByListofID<T>(IList<int> ids) where T : IClient { IList<T> clients = new List<T>(); clients.Add( new Client(3)); } I am getting a compiler error here: cannot convert from 'Bailey.Objects.Client' to 'T' The client object implements the IClient interface. My goal here is to try and loosen t...

generic inheritance in C#?

I can do public class MyGenericClass : DL //but i cannot do public class MyGenericClass <T> : T How would i do the second? if i cannot do that, how can i do something like public class MyGenericClass <T> { T obj; //have all MyGenericClass.XYZ call obj.XYZ } ...

How do I use generics with an array of Classes?

I want to create an array of Classes, each representing a type that is available in the system I'm building. All the Classes involved are subclasses of a common superclass. So I'd like to do: Class<? extends SuperClass>[] availableTypes = { SubClass1.class, SubClass2.class }; This gives me the error: Cannot create a generic array o...

Initializing Generic List In C#

In C# I can initialize a list using the following syntax. List<int> intList= new List<int>() { 1, 2, 3 }; I would like to how that {} syntax works, and if it has a name. There is a constructor that takes an Ienumerable, you could call that. List<int> intList= new List<int>(new int[]{ 1, 2, 3 }); That seems more "standard". When I d...

Using generics with jakarta commons collections Buffer

This code compiles fine in Java <= 1.4. Java 1.6 bitches and moans with the warning: "The method add(Object) belongs to the raw type Collection. References to generic type Collection should be parameterized" import org.apache.commons.collections.Buffer; import org.apache.commons.collections.BufferUtils; import org.apache.commons.collec...

Adding generic object to generic list in C#

I have class where the relevant part looks like class C { void Method<T>(SomeClass<T> obj) { list.Add(obj); } List<?> list = new List<?>(); } How should I define the list so that the class compiles? I want a list of type List<SomeClass<?>>, that is a list of objects of SomeClass where each object can have any type...

How do I detect that an object is a generic collection, and what types it contains?

I have a string serialization utility that takes a variable of (almost) any type and converts it into a string. Thus, for example, according to my convention, an integer value of 123 would be serialized as "i:3:123" (i=integer; 3=length of string; 123=value). The utility handles all primitive type, as well as some non-generic collectio...

Arithmetic operator overloading for a generic class in C#

Given a generic class definition like public class ConstrainedNumber<T>: IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>, IComparable where T:struct, IComparable, IComparable<T>, IEquatable<T>, how can I define arithmetic operators for it? The following does not compile, because the '+'...

Bug in eclipse compiler or javac?

Who is right? Eclipse or javac? --------------- c/v/A.java --------------- package c.v; public class A<T> { } --------------- c/v/B.java --------------- package c.v; public class B extends A<B.Secret> { private class Secret {}; } Eclipse compiles B.java just fine. Javac has a problem. $ javac c/v/B.java c/v/B.java:3: c.v.B.Sec...

Generic List / Dictionary

Hi, I'm trying to output an object graph via reflection. In it there are several generic types (lists, dictionaries). I do not know the types (string, object, etc.) they contain but want to list them (using .ToString()). So, is there a way to output a generic list / dictionary in generic way, that means without writing overloaded func...