covariance

How to declare a method that returns a generic collection of "anything" (C#)

I am using a hierarchy of generic collection classes that derive from an abstract base class to store entity items that also derive from an abstract base class: abstract class ItemBase { } class MyItem : ItemBase { public MyItem() { } } abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { } class ...

Possible to convert IQueryable<Derived> to IQueryable<Base>?

I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Wher...

c# inherit how to override the type of a member in abstract class in child class

I have following code: public abstract class TestProperty { public abstract Object PropertyValue { get; set; } } public class StringProperty: TestProperty { public override string PropertyValue {get;set} } which generate compilation error, I wonder should I have to use generic type in TestProperty in order to achive my goal o...

How to use correlogram to estimate variance?

From a book of computer simulation, I got this two equation. The first is to calculate correlogram, the second is how to use correlogram to estimate variance. The common approach to estimate variance of observation is often not incorrect in computer simulation because observations are often related. My question is, the value I calcu...

Can an array be assigned to an array of an unknown type when you know the name of the field to assign to?

Hey guys, I need to assign an array to a field. I dont know the fields type, but I do have a reference to an instance and the name of the field. I can assume the array can be casted to the fields type. Can this be done? Bas Edit: Hopefully this code will clarify what Im trying to do, this causes an exception in assign: class MyClass...

Understanding Covariance and Contravariance in C# 4.0

I watched a video about it on Channel 9 but I didn't really understand it much. Can someone please give me a simple example about these that's easy to understand? After that maybe how it would be used in practice? ...

Are there any plans for Java to add generic collection covariance?

I was trying to write some code that looked like this: public List<IObject> getObject(){ ArrayList<ConcreteObject> objects = new ArrayList<ConcreteObject>(); return objects; } (Where ConcreteObject implements IObject) This doesn't work at all. It gives a compiler error. Does Java have plans to support this in the future? What is ...

Is List<List<String>> an instance of Collection<Collection<T>>?

I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } ...

Java snippet that causes stack overflow in the compiler or typechecker (javac)?

Yesterday at a seminar the presenter showed a small java program, with 3 classes, featuring both co-variance and contra-variance. When attempting to compile using javac, the type checker will throw a StackOverflowException. The snippet is developed by some guys that work at Microsoft (think one was called Kennedy). Can't find it using...

Extend scala class that extends ordered

I'm having trouble extending a base class that extends Ordered[Base]. My derived class can't extend Ordered[Derived] so can't be used as a key in a TreeMap. If I create a TreeMap[Base] and then just override compare in Derived that works but it's not what I want. I would like to be able to have the derived class as a key. Is there a way ...

Type inference over IEnumerable<T>

There are a few posts already on stack overflow about this sort of thing but not exactly the same - so apologies in advance if this is something that has already been answered. Why does this not work: public class MyBase { } public class MyUtils { public bool Foo<T> (T myObject) { return true; } public bool Foo (MyBase myBaseObject)...

What is a covariant return type?

What is a covariant return type in Java? In object-oriented programming in general? I know, it's a "just Google it"; I did, and found the answer, but I'm asking anyways because of SO's goal to be the #1 Google result for programming questions. ...

Generic type inference with interface inheritance (co(ntra)-variance?) in C# 3

I have the following two generic types: interface IRange<T> where T : IComparable<T> interface IRange<T, TData> : IRange<T> where T : IComparable<T> ^---------^ | +- note: inherits from IRange<T> Now I want to define an extension methods for col...

Contravariance explained

First of, I have read many explanations on SO and blogs about covariance and contravariance and a big thanks goes out to Eric Lippert for producing such a great series on Covariance and Contravariance. However I have a more specific question that I am trying to get my head around a little bit. As far as I understand per Eric's explanat...

.net 4 generics question

Hi, I have the following class structure: public class A : AInterface { } public interface AInterface { } public class B<T> : BInterface<T> where T : AInterface { public T Element { get; set; } } public interface BInterface<T> where T : AInterface { T Element { get; set; } } public class Y : B<A> { } public class Z<T> where...

Is this a covariance problem? Not sure if brick wall.

I wrote ASP.NET pages which will manage forms. They're based on the following base class. public abstract class FormPageBase<TInterface, TModel> : Page, IKeywordProvider where TModel:ActiveRecordBase<MasterForm>, TInterface, new() where TInterface:IMasterForm { public TInterface FormData { get; set; } ...

C# variance problem: Assigning List<Derived> as List<Base>

Look at the following example (partially taken from MSDN Blog): class Animal { } class Giraffe : Animal { } static void Main(string[] args) { // Array assignment works, but... Animal[] animals = new Giraffe[10]; // implicit... List<Animal> animalsList = new List<Giraffe>(); // ...and explicit casting fails Li...

LINQ to SQL Covariance - is this the right way to do this?

I have several database - Elephants, Giraffes, Gorillas, etc - each of which has an Inputs and Results table named ElephantInputs, ElephantResults, GiraffeInputs, GiraffeResults, GorillaInputs, GorillaResults, respectively. I can't control the table naming. I'm using LINQ to SQL to automatically generate the ElephantInputs, ElephantResu...

IDictionary<TKey, TValue> in .NET 4 not covariant

The IDictionary<TKey, TValue> in .NET 4 / Silverlight 4 does not support covariance, i.e. I can't do a IDictionary<string, object> myDict = new Dictionary<string, string>(); analog to what I can do with IEnumerable<T>s now. Probably boils down to the KeyValuePair<TKey, TValue> not being covariant either. I feel that covariance should...

Faster way to cast a Func<T, T2> to Func<T, object> ?

Is there a faster way to cast Fun<TEntity, TId> to Func<TEntity, object> public static class StaticAccessors<TEntity> { public static Func<TEntity, TId> TypedGetPropertyFn<TId>(PropertyInfo pi) { var mi = pi.GetGetMethod(); return (Func<TEntity, TId>)Delegate.CreateDelegate(typeof(Func<TEntity, TId>), mi); } public static Func<...