C# 4.0 is the next version of the C# language being shipped with VS 2010. Personally, I'm still coming to terms with Generics as introduced in C# 2.0 and LINQ which came with 3.0. Anders Hejlsberg touched on the Covariance/Contravariance topic in his TechEd presentation in LA last month entitled "The Future of C#".

Evolution of C# - Anders Hejlsberg

 

The terms Invariance, Covariance and Contravariance are not by any means new. Anyone coming from a computer science background will have encountered the terms. Prior to attending Anders' session, I hadn't given this much conscious thought in a long time - we tend to know what we can and can't do within the syntactical constraints of a language - and tend not to question things further. However, in C# 4.0, delegates and interfaces will play nicer when working with generics. So much attention is likely to be focused on the dynamic programming additions in C# 4.0 (mainly in support of LINQ), that this addition may go unnoticed.

Variance in C# 4.0 - Anders Hejlsberg - Anders Hejlsberg

 

Terminology:

Invariant: A return parameter is invariant if we must use the exact match of the type name. In other words, neither covariance nor contravariance is permitted

Covariant: A parameter is covariant if we can use a more derived type as a substitute for the parameter type. In other words, a subclass instance can be used where a parent class instance was expected.

Contravariant: A return value is contravariant if we can assign the return type to a variable of a less derived type than the parameter. In other words, a super class instance can be used where a subclass instance was expected.

Variance in C# 4.0 - Anders Hejlsberg - Anders Hejlsberg

 

Generally, C# supports covariant parameters and contravariant return types. There has always been support for covariance and contravariance in C# - C# 4.0 will just ensure that generic delegates and interfaces will also behave they way we would expect.

Eric Lippert is the developer responsible for this feature of the C# 4.0 release and has an eleven-part blog series on just this topic. I would also recommend reading Charlie Calvert's article on this topic for some code samples.

Side Note: You do not need VS 2010 to experiment with these features - just download the framework to a test VM.

kick it on DotNetKicks.com

Tags: , ,

.NET 4.0 | C#



What are C# Generics - Part III

by agrace 24. April 2008 18:57

Panama Hat Personally, I think I am still in the process of making the final paradigm leap to generics. It will probably be some time yet before I am satisfied that I am applying the concept creatively enough in the early design phase. The end goal is to become comfortable enough with the new syntax to "genericize" my code and make it more type-safe and reusable overall.

Right now, a lot of people are overly-occupied with the latest 3.5 toys. If it ain't SilverLight or MVC it ain't worth talking about. It pays to concern oneself with the basic building blocks that make up the language of your choice. In my case, it is C# although I used C++ right through college and spent some time with Java. The point is that in an object-oriented world, such concepts as generics travel quite well across languages and underpin the development of a strong object view of things overall. The "drag and drop brigade" frequently have a problem grasping this.

Generic IL and Metadata

Nitty Gritty

Generics operate at the compiler level. It was necessary to change the metadata, compiler and IL instructions to make it all posssible. The upshot is that generics work across different .NET managed languages. This means that we can define a generic class in C# and consume it in VB. Score another one for code reuse :-)

Under the hood, the compiler generates the intermediate language (IL) which contains placeholders for the actual types which will be substituted at runtime. In other words, the generic types are "instantiated" at runtime. This is known as "lazy" specialization or "just-in-time". The accompanying metadata is used for compile-time type-checking and intellisense.

It is possible that code could try to use types which are not compatible with what was intended and "contraints" are provided as a measure of protection to prevent this happening. Remember, a generic class or method knows nothing about the arguments being passed to it. The compiler needs to know something more. Each type parameter can have a different set of constraints, expressed using a where clause. A parameter can have multiple constraints, separated by commas.

class Guitar < T1, T2, T3>
    where T1: Fender
    where T2: Gibson
    where T3: Martin
{
    ...
}


To recap, you will most likely find a use for generics with collections. It is well worth the time invested. Right now, I'm working my way through a copy of Professional .NET 2.0 Generics by Tod Golding. It's about the best book on the topic of Generics that I have come across. Happy coding!

kick it on DotNetKicks.com   PHP, ASP, .NET, JSP Resources, Reviews

Tags: ,

C#



What are C# Generics - Part II

by agrace 11. April 2008 20:49

Generics I'm going to keep this as simple as possible. Your main goal is to gain an appreciation of why we would want to use generics in the first place. The typical candidate scenario for the use of generics is where we have a class that has a member variable, and the type of this variable should be defined by the client of the class, and not by the programmer. Likewise, for a method that has a parameter passed to it. In other words, the code in our classes and methods remains the same and the types of the data can change with each use. Think code reuse.

Up to now, this type of generality involved writing the same code over and over for each type you wanted to accomodate. And with collections, you would typically have to use the object class and then cast back. This would involve boxing and unboxing for value types, which would incur heavy performance hits, depending on the size of your collections. More importantly, when casting back, you could not be certain of the type in the collection. This could result in runtime errors after your code had shipped. More than anything else, the type safety afforded by generics is its biggest selling point. Type-safe code is the easiest code to maintain, plain and simple.

With generics, we declare type-parameterized code which we can instantiate with different types. We write the code with '<T>' placeholders for types and then plug in the actual types when we are creating an instance. We don't have to use the letter 'T', it's just convention.

// 1.1 Loosely-Typed Collection
Class Stack
{
  public object Pop();
  ...
}

Stack s = new Stack();
s.Push(1);
s.Push(2);

// Cast necessary
int i = (int) s.Pop();


// 2.0 Strongly-Typed Collection
Class Stack<T>
{
  public <T> Pop();
  ...
}

Stack<int> s = new Stack<T>();
s.Push(1);
s.Push(2);

// No cast necessary
int i = s.Pop();


You'll probably find the most ready use for generics when implementing collections. The generic collection classes are part of the C# 2.0 System.Collections.Generic namespace. Below is a list of the traditional 1.1 collections and their new 2.0 equivalents (from Krzysztof Cwalina):

Non-Generic Similar Generic Type
ArrayList List<T>
Hashtable Dictionary<TKey,TValue>
SortedList SortedList<TKey,TValue>
Queue Queue<T>
Stack Stack<T>
IEnumerable IEnumerable<T>
ICollection N/A (use IEnumerable<T> or anything that extends it)
N/A ICollection<T>
IList IList<T>
CollectionBase Collection<T>
ReadOnlyCollectionBase ReadOnlyCollectionBase<T>
DictionaryBase N/A (just implement IDictionary<TKey,TValue>)
N/A SortedDictionary<TKey,TValue>
N/A KeyedCollection<TKey,TItem>
N/A LinkedList<T>

In the next part, I'll try and cut through the jargon attached to generics and also discuss some practical uses for generics in everyday coding tasks. In the meantime, check out this excellent Overview of Generics in the .NET Framework.

kick it on DotNetKicks.com   PHP, ASP, .NET, JSP Resources, Reviews

Tags: ,

C#