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...
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...
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 ...
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...
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);...
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...
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...
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...
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
...
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...
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 =...
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 ...
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...
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 ...
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?
...
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...
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> ...
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#?
...
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...
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...