generics

Why doesn't ICollection<T> implement ICollection?

This is a duplicate of: Why IEnumerable<T> inherits from IEnumerable? IList<T> implements IList. (Edit: No it doesn't; I knew this - not sure why I wrote it.) IEnumerable<T> implements IEnumerable. But ICollection<T> does not implement ICollection. What was the rationale for this and/or was it just an oversight? ...

I have a sorted list of key/value pairs, and want to find the values adjacent to a new key

I have a list of key/value pairs (probably will be using a SortedList) and I won't be adding new values. Instead I will be using new keys to get bounding values. For example if I have the following key/value pairs: (0,100) (6, 200), (9, 150), (15, 100), (20, 300) and I have the new key of 7, I want it to return 200 and 150, beca...

How to parameterize a type which extends a type with a wildcard parameter?

I have an interface parametrized on a type which extends a parametrized type, and that interface contains a method which needs the type parameter of the type being extended. In code, this is what I want: class Thingy<X> { ... } interface Foo<P extends Thingy<?>> { <T> T frobnicate(P<T> a); } But that doesn't compile. In order to pr...

Having to implement a generic less than and greater than operation.

I absolutely CANNOT hard code a data type. I need strict data typing. I have to use TValue a <= TValue b. Again, there is ABSOLUTELY NO way to do something like (double) a. This is part of an essential library implementation. The only thing that is specific about the generic values is that they are of static types. IComparable and other ...

List<T> Concatenation for 'X' amount of lists

I have for example 5 List all of the same type. Can I simply do List<T> newset = List1.Concat(List2).Concat(List3).Concat(List4)..... ...

Overriding generic methods with non-generic implementations

I'm experimenting with generics in Java, and thought of this example. If I have ClassA<T>, I can override it with a subclass that references a concrete class, such as ClassB extends ClassA<String>, then anywhere ClassA uses T, ClassB can use a String. Now, ignoring the previous ClassA and ClassB, if I have an abstract ClassA, which has...

Using generics in abstract classes.

I'm working on an abstract class where the implementing class needs to implement a list of T. The problem is that this doesn't work: public class AbstractClass { public int Id { get; set; } public int Name { get; set; } public abstract List<T> Items { get; set; } } public class Container : AbstractClass { public List<W...

Is it possible to simultaneously and generically subclass both a bounded generic class and a generic interface?

I'm trying to create a new class by subclassing another generic class (with a bound) and implementing a generic interface (without a bound): public class Foo1<T extends Bar> { ... } public interface Foo2<T> { ... } public class ProblemClass<T extends Bar, U> extends Foo1<T extends Bar> implements Foo2<U> { ... } ...

What is the cleanest way to define this generic interface so it compiles?

Since, two methods with the same parameters but different return values will not compile. What is the best way to define this interface without loosing clarity? public interface IDuplexChannel<T, U> { void Send(T value, int timeout = -1); void Send(U value, int timeout = -1); bool TrySend(T value, int timeout = -1); ...

XmlSerializer is throwing InvalidOperationException when using the generic type constraint where

When I try to run the following code (two separated assemblies) ClassLibrary.cs public interface ITest { } Program.cs using System; public class TestClass { public void Test<T>(T x) where T : ITest { } } static class Program { static void Main(string[] args) { new System.Xml.Serialization.XmlSerialize...

How do I implement IDictionary(Of TKey, TValue) in VB?

I want to create an implementation of the IDictionary(Of TKey, TValue) interface to provide some wrapper functionality. Public Class ExtendedDictionary(Of TKey, TValue) Implements IDictionary(Of TKey, TValue) Private _Dictionary As Dictionary(Of TKey, TValue) Public Sub Add(ByVal key As TKey, ByVal value As T) Implements I...

How can I create a generic method to convert lists of objects when conversion methods for single objects exist?

Assume some domain and view objects (that have no common base class) public class DomainA { public int MyProperty { get; set; } } public class ViewA { public int MyProperty { get; set; } } public class DomainB { public string MyProperty { get; set; } } public class ViewB { public string MyProperty { get; set; } } an...

NUnit TestCase with Generics

Is there any way to pass generic types using a TestCase to a test in NUnit? This is what I would like to do but the syntax is not correct... [Test] [TestCase<IMyInterface, MyConcreteClass>] public void MyMethod_GenericCall_MakesGenericCall<TInterface, TConcreteClass>() { // Arrange // Act var response = MyClassUnderTest.My...

Specific generic interfaces

I'm refactoring all my repository interfaces of various types. Most of them contain very similar methods like Add, Update but some have methods which only makes sense for a specific type. This is a best practices question. I thought about using generics to straighten things up. public interface IRepository<T> { T Get(int id); ...

How do you get an instance of java.lang.Class for a generic collection like Collection<SomeObject>?

How do you get an instance of java.lang.Class for a generic collection like Collection<SomeObject>? I'm looking for something like the following: Class clazz = Collection<SomeObject>.class; ...

Generic Typed Inner Classes in Java

Hello, I've been learning and experimenting with Java Generics for a while but I have run into something that I cannot explain. Take for example the following code: public class Question { public <T> Sub<T> getSub(Class<T> c) { return new Sub<T>(c); } public class Sub<S> { private Class<S> c; public ...

Generic Java 5 method that can take either a Class or a Collection?

I have a logic where I am building XML data in a method. The XML data is built off either a ArrayList or a ModelClass. For both of these, the logic to build XML is exactly the same. Currently I have 2 separate methods to handle this and thus code duplication. I want to use 1 single method which can take either a collection or a class as ...

A generic linear interpolator: how to cope with DateTime?

I'd like to write a LinearInterpolator class, where X is the type of the X axis value, and Y the type of the Y axis value. I can't see how to do this such that X could be a DateTime or a double. The class is something like below (which is untested): class LinearInterpolator<X, Y> { private List<X> m_xAxis; private List<Y> m_yAxi...

Creating a Generic Dictionary From a Generic List

I am trying to do the following but I think I must be missing something...(fairly new to generics) (Need to target .NET 2.0 BTW) interface IHasKey { string LookupKey { get; set; } } ... public static Dictionary<string, T> ConvertToDictionary(IList<T> myList) where T : IHasKey { Dictionary<string, T> dict = new Dictionary<stri...

Value of 0 in Hibernate IndexColumn mapped with base=1

We are using Hibernate Annotations 3.4.0GA and Hibernate Core 3.3.2.GA (also known as the current stable versions) against an Oracle database We have a One-to-Many mapping with base=1 which worked fine for a loooong time, yet last week we found some entries in the database where the index column contained a value of 0 which caused all k...