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...
I am having trouble understanding the difference between covariance and contravariance.
My understanding is one supports out, as in "action" with no return type, which is casting to. The other takes in an argument and is casting from, is this correct?
...
Generic Variance in C# 4.0 has been implemented in such a way that it's possible to write the following without an exception (which is what would happen in C# 3.0):
List<int> intList = new List<int>();
List<object> objectList = intList;
[Example non-functional: See Jon Skeet's answer]
I recently attended a conference where Jon Ske...
Hi there,
I again need help by you, this time I struggle with covariance, contravariance, delegates and an simple idea blowing up...
I want to implement an attribute for our businessobject-properties that takes a delegate and the needed parameters for that one, so that I can work with reflection, read out the attribute and perform a va...
I'm hashing a file with one or more hash algorithms. When I tried to parametrize which hash types I want, it got a lot messier than I was hoping.
I think I'm missing a chance to make better use of generics or LINQ. I also don't like that I have to use a Type[] as the parameter instead of limiting it to a more specific set of type (H...
What is the real reason for that limitation? Is it just work that had to be done? Is it conceptually hard? Is it impossible?
Sure, one couldn't use the type parameters in fields, because they are allways read-write. But that can't be the answer, can it?
The reason for this question is that I'm writing an article on variance support in ...
I don't understand why 'x' below converts, but 'y' and 'z' do not.
var list = new List<List<int>>();
IEnumerable<List<int>> x = list;
List<IEnumerable<int>> y = list;
IEnumerable<IEnumerable<int>> z = list;
Does the new covariance feature simply not work on generics of generics or am I doing something wrong? (I'd like to avoid using ...
Hi,
I'm at the point where I'm starting to grasp contravariance, although I'm trying to work out what the advantage is when an interface can be used instead. Obviously I'm missing something.
Here is the c#4 example
class Dog : Animal {
public Dog(string name) : base(name) { }
}
class Animal {
string _name;
...
I've come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context.
Is there a good concise explanation of what they are and what they are useful for out there?
Edit for clarification:
Covariant interface:
interface IBibble<out T>
.
.
Contravariant interface:
int...
When we define our interfaces in C# 4.0, we are allowed to mark each of the generic parameters as in or out. If we try to set a generic parameter as out and that'd lead to a problem, the compiler raises an error, not allowing us to do that.
Question:
If the compiler has ways of inferring what are valid uses for both covariance (out) an...
Here's the scenario i am faced with:
public abstract class Record { }
public abstract class TableRecord : Record { }
public abstract class LookupTableRecord : TableRecord { }
public sealed class UserRecord : LookupTableRecord { }
public interface IDataAccessLayer<TRecord>
where TRecord : Record { }
public interface ITable...
Just a stupid question. I could try it out in 2 minutes, really. It's just that I have 1 GB RAM and have already got 2 instances of VS 2010 open on my desktop, with an instance of VS 2005, too. Opening another instance of VS 2010 would be an over kill.
Can I have a type (for now forgetting its semantics) that can be covariant as well as...
If I have a class that raises an event, with (e.g.) FrobbingEventArgs, am I allowed to handle it with a method that takes EventArgs?
Here's some code:
class Program
{
static void Main(string[] args)
{
Frobber frobber = new Frobber();
frobber.Frobbing += FrobberOnFrobbing;
frobber.Frob();
}
private static ...
Is there a way to reflect on an interface to detect variance on its generic type parameters and return types? In other words, can I use reflection to differentiate between the two interfaces:
interface IVariant<out R, in A>
{
R DoSomething(A arg);
}
interface IInvariant<R, A>
{
R DoSomething(A arg);
}
The IL for both looks the...
What does the statement mean?
From here
ref and out parameters in C# and
cannot be marked as variant.
1) Does it mean that the following can not be done.
public class SomeClass<R, A>: IVariant<R, A>
{
public virtual R DoSomething( ref A args )
{
return null;
}
}
2) Or does it mean I cannot have the followi...
Pretty self explanatory question, glad for any answer. For me it doesn't make sense. I don't care about losing information. I just want to concatenate all my lists that inherit IRenderable somehow into a single list and then call IRenderable methods on it.
I have a feeling that this is some basic rule of programming I forgot...
edit:
T...
So now that we have generic Covariance and Contravariance on interfaces and delegates in C#, I was just curious if given a Type, you can figure out the covariance/contravariance of its generic arguments. I started trying to write my own implementation, which would look through all of the methods on a given type and see if the return type...
Possible Duplicate:
Why isnt there generic variance for classes in C# 4.0?
As a rookie programmer I have a couple of questions about variance in .NET 4. Not so much about how it works, but why certain things are not variant and if other people would find this useful.
Question 1:
I know that interfaces and delegates can be c...
Can anyone shed light on why contravariance does not work with C# value types?
The below does not work
private delegate Asset AssetDelegate(int m);
internal string DoMe()
{
AssetDelegate aw = new AssetDelegate(DelegateMethod);
aw(32);
return "Class1";
}
private static House DelegateMethod(object m)
{
return null;
}
...
I am just starting to read about category theory, and would very much appreciate it if someone could explain the connection between CS contravariance/covariance and category theory. What would some example categories be (i.e. what are their objects/morphisms?)? Thanks in advance?
...