generics

C# Generics: Inserting T as the where T : IComparable<T> interface conflict

This is a challenge for the C# generics / design patterns masters. I'm trying to implement a generic heap, and then a priority queue that uses the heap. My heap's signature is: class Heap<TKey, TValue> where TKey : IComparable<TKey> My priority queue class is: public delegate IComparable<T> Evaluator<T>(T item); class PriorityQueu...

Is it bad practice to use generics as return values from functions?

Somebody told me this but i have not seen this anywhere and i have used it all over, i don't see why would it be bad practice. Example of what i mean is having functions such as: public List<SomeCustomeType> GetListOfStuff() { } or public void DoSomeStuff(List<SomeCustomeType> param) { } can anyone tell me why would this be bad p...

Generics with Grails

What I am trying to do is define a list which asks for a specific type (List<Integer>). During the initialization of the class I put in a list of String I expect it to throw some runtime casting error. But it doesn't - it runs fine. This is probably grails 101 stuff im sure but could someone explain why this works and also how I would ...

Events handling with generic handlers in java

Hello I have written custom dispathing/handling event system that generally look like this: Event handler interface: public interface EventHandler{ } Base event class: public abstract class Event<H extends EventHandler> { public static Class Type<H> { } public abstract void dispatch(H handler); } Handler manager: public c...

Why can't the C# constructor infer type?

EDIT: updated the question after PostMan pointed out the error on my part Just out of curiosity, does anybody know why type inference is not supported for constructor the way they are for generic methods? i.e. public class MyType<T> { private readonly T field; public MyType(T value) { field = value; } } var obj = new MyType(42);...

How to create a Generic DAO class using Hibernate Context sessions

I'm trying to implement a Generic DAO using the Hibernates Context Sessions. Following was my shot:| import java.io.Serializable; public interface GenericDao<T, ID extends Serializable> { /** Persist the newInstance object into database */ ID create(T newInstance); /** * Retrieve an object that was previously persisted to the da...

Generic method returning generic interface in Delphi 2010

Given the code below, wich is a very trimmed down version of the actual code, I get the following error: [DCC Error] Unit3.pas(31): E2010 Incompatible types: 'IXList<Unit3.TXList<T>.FindAll.S>' and 'TXList<Unit3.TXList<T>.FindAll.S>' In the FindAll<S> function. I can't really see why since there is no problem with the previous ver...

Can't cast to to unspecific nested type with generics

I have two classes with nested generics. Is there a way to get rid of the Type mismatch: cannot convert from Msg<Value<String>> to Msg<Value<?>> error ? In the last assignment public class Value<V> { V val; public Value(V val) { this.val = val; } @Override public String toString() { return "" + va...

C# Generics syntax help

Hi, I can't figure out the correct sytax for the following: public interface IRepository<T,E> where T E: class Looked a lot online, but articles don't seem to cover two classes. Thank you ...

Jaxb marshaller ang generics(2)

there are types: class A{} @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) @XmlType(propOrder = {"obj"}) @XmlRootElement(name = "response") public class B<T extends A> extends A{ private T obj; @XmlElement(required = true) public T getObj() { return obj; } } When i'm trying to marshal this i get an error: org.springframew...

Non-static method requires a target in PropertyInfo.SetValue

Ok, so I'm learning about generics and I'm trying to make this thing run, but its keep saying me the same error. Here's the code: public static T Test<T>(MyClass myClass) where T : MyClass2 { var result = default(T); var resultType = typeof(T); var fromClass = myClass.GetType(); var toProperties =...

How to implement this logic in linq, collection . Shortning a searching scope

PK_RPM_BTN_SETTING_ID FK_BUTTON_ID FK_ITEM_OR_PKG IS_ITEM_OR_PKG SORT_ORDER --------------------- ------------ -------------------- -------------- ---------- 1 1 1 I 1 1 2 1 P 2 1 3 ...

using static property of an abstract class inside a generic class that works on subclasses of the abstract class

Hi, I have a question regarding the following code: abstract class a { public static string x; } class b<c> where c : a { public void f() { c.x=10; } } This code does not compile. I get an error at the statement c.x=10; . The problem makes it look as if the condition where c:a does not have any effect at all...

Simple Generics -- Is this possible Method<typeof(T)>()?

I am attempting to create a generic mapping function that takes the values of a dictionary and its sub classes and maps them to the fields in T using reflection. Within object T, there are sub objects that must be drilled into, and through recursion, it seems like a pretty simple concept. However I am stuck -- and I'm not sure if it's ...

Why doesn't IEnumerable<T> implement Add(T)?

Just now find it by chance, Add(T) is defined in ICollection<T>, instead of IEnumerable<T>. And extension methods in Enumerable.cs don't contain Add(T), which I think is really weird. Since an object is enumerable, it must "looks like" a collection of items. Can anyone tell me why? ...

Nullable primitives with Generics

I'm trying to find the right thing to do with a non-null validation on a nullable boolean. I also want to be able to do the same thing with some other fields, including strings, ints, etc., so I want to use generics for the method. Here's an example of the kind of thing which can happen. bool? myValue = null; bool valid = ValidateNotNul...

Can I create a Generic delete method to delete any subsonic activerecord<T> object?

We have a C# WinForms app with a number of screens that support CRUD operations. We've been trying to create a generic method in the base form class to delete any subsonic ActiveRecord class. This has proved to be a challenge. calling Delete() for a subsonic type involves calling the static method on the abstract base ActiveRecord<T> ...

Mnemonic for C# generic types

I often forget if i have to use in or out when defining covarient and contravarient generic types. In java i have the mnemonic PECS (producer extends consumer super) to help me. Do you know a similar mnemonic for c#? ...

How does Scala's (2.8) Manifest work?

I have some Scala code that makes fairly heavy use of generics, and I have gleaned from the docs that using a manifest in the parametrization constraints can help me work around the type erasure issues (e.g. I want to instantiate a new object of the generic type). Only, I'd like to understand more about how this works. It almost feels li...

Any way to return an object which does not exist yet but will later from a C# method?

Disclaimer 1: Crazy pedantic language-bending drivel ahead. Disclaimer 2: To any clients present or future - I am not billing you for this. Alright, so this is not really necessary but I'm messing around with creating plugins for xunit.net and an interesting scenario comes up Currently, the example of the SubSpec extension that shi...