generics

C#: How to remove items from the collection of a IDictionary<E, ICollection<T>> with LINQ?

Here is what I am trying to do: private readonly IDictionary<float, ICollection<IGameObjectController>> layers; foreach (ICollection<IGameObjectController> layerSet in layers.Values) { foreach (IGameObjectController controller in layerSet) { if (controller.Model.DefinedInV...

java generics and the addAll method

What is the correct type of argument to the addAll(..) method in Java collections? If I do something like this: List<? extends Map<String, Object[]>> currentList = new ArrayList<Map<String, Object[]>>(); Collection<HashMap<String, Object[]>> addAll = new ArrayList<HashMap<String, Object[]>>(); // add some hashmaps to the list.. currentL...

A generic list of generics

I'm trying to store a list of generic objects in a generic list, but I'm having difficulty declaring it. My object looks like: public class Field<T> { public string Name { get; set; } public string Description { get; set; } public T Value { get; set; } /* ... */ } I'd like to create a list of these. My problem...

Do I have to allocate and free records when using TList<T> in Delphi?

The question more or less says it all. Given the following record structure: type TPerson = record Name: string; Age: Integer; end; PPerson = ^TPerson; TPersonList = TList<TPerson>; Is the following code valid? procedure ReadPeople(DataSet: TDataSet; PersonList: TPersonList); begin PersonList.Coun...

C# ambiguity in Func + extension methods + lambdas

I've been trying to make my way through this article: http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx ... And something on page 1 made me uncomfortable. In particular, I was trying to wrap my head around the Compose<>() function, and I wrote an example for myself. Consider the following two Func's: Func<d...

Serialization of generic types - GWT

I have an interface like this public interface IField<T> extends IsSerializable { public String getKey(); public void setKey(String name); public T getValue(); public void setValue(T role); } And a class like this public class FieldImpl<T> implements IField<T> { private String key; public String getKey() { return key; } public...

How do I cast from int to generic type Integer?

I'm relatively new to Java and am used to generics in C# so have struggled a bit with this code. Basically I want a generic method for getting a stored Android preference by key and this code, albeit ugly, works for a Boolean but not an Integer, when it blows up with a ClassCastException. Can anyone tell me why this is wrong and maybe he...

How to check if TypeIdenitifier(T) is an Object?

I'm creating a generic list class that has a member of type Array(Array of ). The problem is the class destruction,because the class is supposed to be used for types from byte to types inheriting TObject. Specifically: destructor Destroy; var elem:T; begin /*if(T is Tobject) then //Check if T inherits TObject {Compiler error!} f...

Why is Delphi unable to infer the type for a parameter TEnumerable<T>?

Consider the following declaration of a generic utility class in Delphi 2010: TEnumerableUtils = class public class function InferenceTest<T>(Param: T): T; class function Count<T>(Enumerable: TEnumerable<T>): Integer; overload; class function Count<T>(Enumerable: TEnumerable<T>; Filter: TPredicate<T>): Integer; overload; end; So...

Constraint Validation

I am using javax.validation.Validator and relevant classes for annotation based validation. Configuration<?> configuration = Validation.byDefaultProvider().configure(); ValidatorFactory factory = configuration.buildValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<ValidatableObject>> const...

Delegate.CreateDelegate() and generics: Error binding to target method

I'm having problems creating a collection of delegate using reflection and generics. I'm trying to create a delegate collection from Ally methods, whose share a common method signature. public class Classy { public string FirstMethod<T1, T2>( string id, Func<T1, int, IEnumerable<T2>> del ); public string SecondMethod<T1, T2>( strin...

Is there any way to accomplish something like List<object> list = new List<int>() in C#?

That is the question. What I want to accomplish is something similar to the following Java code: class A { } class B extends A { } public class Tests { public static void main(String [] args) { ArrayList<? extends A> lists = new ArrayList<B>(); } } (in which B extends A means B inherits from A) Is it possible at all...

Inferring type from method generics

I am from a Java background and I am looking from the equivalent in c# for the following. public interface Reader { <T> T read(Class<? extends T> type); } Such that I can do the following, constraining the parameter and inferring the return type. Cat cat = reader.read(Cat.class); Dog dog = reader.read(Dog.class); I was hoping so...

Class hierarchy problem (with generic's variance!)

The problem: class StatesChain : IState, IHasStateList { private TasksChain tasks = new TasksChain(); ... public IList<IState> States { get { return _taskChain.Tasks; } } IList<ITask> IHasTasksCollection.Tasks { get { return _taskChain.Tasks; } <-- ERROR! You can't do this in C#! ...

Ambiguous Generic restriction T:class vs T:struct

This code generates a compiler error that the member is already defined with the same parameter types. private T GetProperty<T>(Func<Settings, T> GetFunc) where T:class { try { return GetFunc(Properties.Settings.Default); } catch (Exception exception) { SettingReadE...

Code explanation in Java

Hello everyone, this morning I came across this code, and I have absolutely no idea what that means. Can anyone explain me what do these <T> represent? For example: public class MyClass<T> ... some bits of code then private Something<T> so; private OtherThing<T> to; private Class<T> c; Thank you ...

Why don't Generics support primitive types?

Why Generics (in Java) works with the objects but not with primitive types? For example Gen<Integer> inum = new Gen<Integer>(100); // works fine, but Gen<int> inums = new Gen<int>(100); // is not allowed. Thanks ! ...

Java Generics: What is PECS?

Hi, I came across PECS (short for Producer extends and Consumer super) while reading on Generics. Can someone explain to me how to use PECS to resolve confusion between extends and super? Thanks in advance! ...

Using Covariance with an Interface Base Type in .NET 4?

I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily in drop-down lists) implement an interface I've called IValue. I did this because the UI layer is going to have to account for a couple special cases -- notably, what to display if the original value on a record has been flagged as deleted...

Question about C# 4.0's generics covariance

Having defined this interface: public interface IInputBoxService<out T> { bool ShowDialog(); T Result { get; } } Why does the following code work: public class StringInputBoxService : IInputBoxService<string> { ... } ... IInputBoxService<object> service = new StringInputBoxService(); and this doesn't?: public class I...