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#




There are times when you will want to set the color of the grid lines in your GridView - however there is not to my knowledge a way of doing this declaratively. A workaround is to do this by tapping into the GridView's RowDataBound event.

GridView Gridlines

 

First, set the OnRowDataBound property in the markup of the GridView:

OnRowDataBound="MyGrid_RowDataBound"

 

Second, set the color in the OnRowDataBound handler method:

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
     foreach (TableCell tc in e.Row.Cells)
     {
         tc.Attributes["style"] = "border-color: #c3cecc";
     }
}

 

Happy coding :-)

Update 06-01-2009:
See Lee's suggestion in the comments on a quicker way to do this:
Add this to the page Load method: this.GridView1.Attributes.Add("bordercolor", "c3cecc");

kick it on DotNetKicks.com  411ASP.NET



Joining the Twitter Bandwagon

by agrace 27. May 2009 06:16

Follow me on Twitter Well, I've finally succumbed to the World of Twitter. How did I ever live without it? Okay, sarcasm aside, it's fun to play with and what really sold me was Scott Hanselman's blog post on how it can provide us with an alternative to going through the 1-800 recorded messages from hell when trying to get customer support - the idea being that you have a fair chance of getting some attention the next time your cable acts up by mentioning it in Twitter. Apparently big companies are monitoring the big T by using its search facility. Ever wonder what's being said about you?

After signing up and spending an inordinate amount of time trying to conjure up something deep and meaningful under 140 characters, I downloaded TweetDeck. Then I signed up to http://bit.ly which basically provides the same URL-shortening service as http://tinyurl.com, except better. I have yet to check out TBuzz, Twitmatic, Twubs, TweetMic, Tweekdoku, Twadget, Twit4Live, Twittervision, Twitterholic........

Follow me on Twitter

 

It is estimated that there are currently over six million people "tweeting". More and more developers are using it to break news items and get quick answers to coding problems. You can find some of the top names in development circles using Twitter here.

Follow me on Twitter! I will include the Twitter logo in the new CodersBarn facelift.... soon.