posts - 94, comments - 121, trackbacks - 82

My Links

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites

Article Categories

Archives

Post Categories

Image Galleries

Other Links

Simple explanation for co-variance and contra-variance

Co-variance and contra-variance have always been two terms for me that made me pause.  Neither are hard to understand and we often use what they mean instinctively in object-oriented programming.  However, before today if you were to ask me what they were I would have to give you an example.  For some reason, a clear, concise definition escaped me.

Then today I read a blog post on C# v4.0 on the Intel website that summed it up in one of those "ah ha" and "duh" moments.

Co-variance is when you treat something more derived as something less derived
Contra-variance is when you treat something less derived as something more derived

And the examples (not designed to model anything real) are easy:

// co-variance
private void DoSomething(object[] objects) { ... }

string[] myStrings = new string[] { "a", "b", ... };
DoSomething(myStrings);

// contra-variance
private void DoSomething(MyConcreteObjectA a) { ... }

private void DoSomething(MyConcreteObjectB b) { ... }

private MyObjectBase CreateObject() { ... }

MyObjectBase newObject = CreateObject();
DoSomething(newObject);

Print | posted on Monday, April 13, 2009 2:32 PM | Filed Under [ .NET ]

Feedback

Gravatar

# re: Simple explanation for co-variance and contra-variance

you really suck
9/18/2009 4:56 AM | Armin
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: