generics

Best c# generics class for replacing DataTable as collection?

I'm trying to bring a legacy C# .NET 1.1 application into the modern era. We use DataTables for our collections of what could have been business objects. Given that most of the code thinks it is talking to the interface of a DataRow, what generic collection would make for the least painful transition? ...

Casting to a Class which is determined at run-time

I have a method fetchObjects(String) that is expected to return an array of Contract business objects. The className parameter tells me what kind of business objects I should return (of course this doesn't make sense in this construed case because I already said I will return Contracts, but it's basically the situation I have in my real ...

Generics: What's a "CONSTRUCTOR constraint"?

I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this: interface TMyDataList<T: TBaseDatafile> = class(TObjectList<TBaseDatafile>) public constructor Create; procedure upload(db: TDataSet); end; implementation constructor TMyDataList<T>.Create; beg...

What is the easiest alternative to a generic array in Java?

Suppose I have this: class test<T> { private T[] elements; private int size; public test(int size) { this.size = size; elements = new T[this.size]; } } It seems this isn't possible because the compiler doesn't know what constructor to call once it tries to replace the generics code or something. Wha...

Can I Limit the depth of a Generic Stack?

Is there a built in way to limit the depth of a System.Collection.Generics.Stack? So that if you are at max capacity, pushing a new element would remove the bottom of the stack? I know I can do it by converting to an array and rebuilding the stack, but I figured there's probably a method on it already. EDIT: I wrote an extension method...

What is the fully qualified name of a C# method?

In any class, how do I explicitly refer to a certain method of my class? For example, this code works: class Test : IEnumerable<T> { public IEnumerator<T> GetEnumerator() { return null; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } But this one doesn't! class Test : IEnumerable<T> { IEnumerator<...

How do I make a generic object list work from a specific base class?

TMyDataList<T: TBaseDatafile, constructor> = class(TObjectList<TBaseDatafile>) public constructor Create; procedure upload(db: TDataSet); end; I read in a blog post (I don't remember where now) that this is the way to declare a generic-based class with a specific base type for the generic object. And the compiler ...

How to dynamically create a object of a generic class?

i have a sub with this signature Public Sub Save(ByVal obj As IPta) now i want the create a instance of Generic.PtaDao(of T) with the type of obj, which could be anything that inherits from my base class Pta how can i do that? i already found a c# example http://stackoverflow.com/questions/307984/declare-a-generic-type-instance-dynam...

Initialize generic object with unknown type

How can I initialize a list containing generic objects whose types can be different? For example, I have the following: this.Wheres = new List<Where<>>(); As you know, <> is not valid syntax. However, sometimes the type passed to Where will be a string and sometimes it will be DateTime, etc. I tried using object as the initialized ty...

Java Generics - difficulty enforcing strong type checking

Here's my code: public class Sequence<T> { protected List<T> sequence = new ArrayList<T>(); public Matrix<OrderedPair<T, ?>> createCartesianProduct(Sequence<?> secondSequence) { Matrix<OrderedPair<T, ?>> result = new Matrix<OrderedPair<T, ?>>(); for (int rowIndex = 0; rowIndex < sequence.size(); rowIndex++) { S...

What does System.Collections.Generic.Dictionary<T,T>.Equals actually do?

I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual, expected; actual = new System.Collections.Generic.Dictionary<int, string> { { 1, "foo" }, { 2, "bar" } }; expected = new System.Collections.Generic.Dictionary<int, string> { { 1, "foo" }, { 2, "bar" } }; Assert.AreEqu...

Why can't the Java compiler figure this out?

Why is the compiler unable to infer the correct type for the result from Collections.emptySet() in the following example? import java.util.*; import java.io.*; public class Test { public interface Option<A> { public <B> B option(B b, F<A,B> f); } public interface F<A,B> { public B f(A a); } public ...

Why is Java's Cloneable Interface Not Generic?

Java 5 introduced generics, and they were added to many interfaces in the java.lang package. However, Cloneable did not get generics. I wonder why? Edit: In reply to the answers of @Jon and @litb, and the comment of @Earwicker, I was thinking Cloneable might be: public interface Cloneable<T> { public T clone(); } Here T clone()...

How to make a .Net generic method behave differently for value types and reference types?

I have two implementations of a method, one for value types and another for reference types: public static Result<T> ImplRef(T arg) where T : class {...} public static Result<T> ImplVal(T arg) where T : struct {...} I want to write a method which calls the correct implementation like this public static Result<T> Generic(T arg) { ...

Generic Parse Method without Boxing

I am trying to write a generic Parse method that converts and returns a strongly typed value from a NamedValueCollection. I tried two methods but both of these methods are going through boxing and unboxing to get the value. Does anyone know a way to avoid the boxing? If you saw this in production would you not like it, how bad is it f...

Can't operator == be applied to generic types in C#?

According to the documentation of the == operator in MSDN, For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares t...

One function with varying return types ... possible with Generics?

I have a few procedures, for simplicity sake, look like the following: public string FetchValueAsString(string key) public int FetchValueAsInteger(string key) public bool FetchValueAsBoolean(string key) public DateTime FetchValueAsDateTime(string key) I know I could just have one method that returns and object type and just do a conve...

Converting a generic argument to an int in java, provided that it is a number.

Here's what I've been trying to do, in a nutshell: class example <T extends Number> { private int function(T number) { int x = (int) number; ... } ... } Basically, I'm trying to make it so that T is a number so I can convert it to an int inside that function. The problem is that I'm getting an "incovert...

Java cast error trying to create a generic array of custom made class.

This is what I'm trying to do: import java.lang.reflect.*; class exampleOuter <T extends Number> { private exampleInner<T>[] elements; public exampleOuter(Class<T> type, int size) { elements = (exampleInner<T>[]) Array.newInstance(type, size); } } I was told that if I wanted to create generic arrays of type T...

Java doesn't seem to be comparing Doubles right.

I created a Linked List, with insert, search and remove functions. I also created a iterator for it. Now, suppose I do this: myList<Integer> test = new myList(); test.insert(30); test.insert(20); test.insert(10); myList.iterator it = test.search(20); if(it.hasNext()) System.out.println(it.next()); And voila, it works (it prints th...