Posts
49
Comments
83
Trackbacks
0
September 2009 Entries
Covariance in .NET 4.0

On Monday I presented at the St. Louis .NET User Group, and the topic was ‘What’s new in VS2010 and .NET 4.0 Framework’.  Within the whirlwind look at all the new and cool stuff coming in the near future, the subject of Covariance and Contravariance support was discussed.  Given the time constraint I only spent 3-4 minutes at it.  I alluded to the group that the concept of Covariance/Contravariance is rather hard to understand, because it is not as intuitive as it could be; I just asked them to trust me that the support is there.

After the session, talking with some people, apparently the concept is REALLY hard to understand; and in hindsight I should’ve known this too.  It took me a good 3-4 hours of fiddling around and research to get the concept embedded in my head so it will take others the same amount of time to understand it too.

Given that the concept is rather hard, I thought I should blog about it so I don’t forget it in the future .  In this post, I’ll talk about Covariance only and I’ll post about Contravariance in the future.

Let’s start at the very, very basic level: the word itself.  Variance.  From this article on Wikipedia, I managed to synthesize that the word variance means ‘conversion of types’.  Covariance is the conversion of a type from more specific to more general.  Contravariance denotes the reverse: conversion of a type from more general to more specific.

Let’s do a simple case:

private void Test()
{
   string str = "Test";
   ProcessObject(str);
}

private void ProcessObject(object obj)
{
   // Some code...
}

In the code above we have a simple case where the consuming code declares a string, and then passes the string into a method that accepts an object.  That is Covariance in action: conversion of a type from a more specific one to a more general one (string to object).  Of course, the term doesn’t pop into mind; as developers we just see that the operation is valid since we’re passing a variable to a method that can accept the base type.  So, to go 1 step further, the following is also valid:

public class Base
{
}

public class Derived : Base
{
}

private void Test2()
{
   Derived d = new Derived();
   ProcessBase(d);
}

private void ProcessBase(Base b)
{
   // Some code...
}

As you can see, this is simple OOP in action; we’re passing an instance of the Derived class (a more specific type) to a method that can accept the Base class (a more general type).  Everything is logical, and makes sense.  Covariance in action.

So let’s push further 1 more step; we’re going to do covariance (conversion of type from more specific to more general), but without methods this time.

private void Test3()
{
   string[] strs = new string[5];
   object[] objs = strs;

   objs[0] = "Test";
   objs[1] = 7;  // This will crash the application
}

In the above code, this conversion of arrays is fully legal; however the last line will cause the program to crash.  The array conversion from string[] to object[] is not very common, but it does work.  However, since you can also use the array to assign values, this conversion is considered to be an unsafe covariance, since it will only work for valid assignment, but it cannot be checked at compile-time.

So, let’s take it 1 step further again:

private void Test4()
{
   List<string> strs = new List<string>();
   List<object> objs = strs;  // Why not?

   // To prevent the following code:
   objs.Add(5);
}

The above code won’t compile, the second line ( List<object> objs = strs; ) will fail with the error that it cannot implicitly convert List<string> to List<object> – it’s how it has been in .NET.  The initial question is why is this not allowed?  It’s because if we’re allowing the conversion, then the line afterwards (which is very illegal and we don’t want to have that kind of code ever) will crash the application definitely, and it’s not something that can be checked during compile-time.  Very similar to the prior example, but developers will use generics more so than arrays, thus this is not allowed.

In both cases, the application will break because both constructs (array and List<T>) allows assigning of values into the container.  But what if we don’t allow assigning values?  Let’s see if we can create such a scenario:

public interface IOutOnlyList<T>
{
   T this[int index] { get; }
}

public class NewList<T> : List<T>, IOutOnlyList<T>
{
}

private void Test5()
{
   IOutOnlyList<string> strs = new NewList<string>();
   IOutOnlyList<object> objs = strs;  // This should now be valid!

   // Can't assign to objs, can only get value out
   object temp = objs[0];
}

So we’re trying to create a construct just for our own use here: we have an interface (IOutOnlyList<T>) that provides a get-only indexer.  We need to have this interface implemented, so we created the NewList<T> class which inherits from List<T> and also implements IOutOnlyList<T>.  In the above code (which still won’t compile due to the assignment of strs to IOutOnlyList<object>), we’re showing that with the IOutOnlyList<T> interface, the code cannot make changes to the container; as such it should be legal.  There’s no way to write code to break the application now that’s not detectable at compile time.

That’s exactly the argument as to why this type of covariance should be allowed.  In .NET 4.0, this is now possible:

  • If the construct only allows getting the values out only
  • And the construct does not allow changing the values contained within

The code above will be legal in .NET 4.0, with the following minor change:

public interface IOutOnlyList<out T>
{
   T this[int index] { get; }
}

The change is in the signature of the generic type T, we provide an out keyword.  That out keyword tells the compiler that the interface (or class) will only use the generic type T as output, but never as input.  This allows the compiler to verify that the interface is indeed conforming to that specification (T is never used as input) and it will then allow covariance for IOutOnlyList<T> from a more specific type (string) to a more generic type (object).

The example so far is for our own interfaces; to allow covariance .NET 4.0 changes the following constructs (by using the out keyword):

Because of these changes, the code below will now work; you can assign variables of type IEnumerable<string> to type IEnumerable<object>. Since the conversion works, you can also use this to pass in parameters into methods as well.

private void Test6()
{
   IEnumerable<string> strs = new List<string>();
   IEnumerable<object> objs = strs;  // IEnumerable can't change values contained

   ProcessObjects(strs);
}

private void ProcessObjects(IEnumerable<object> objs)
{
   // Do something with objs
}

That’s the idea of Covariance in .NET 4.0 and how it is used.  Contravariance is the reverse of Covariance, but the explanation takes a bit longer…

posted @ Wednesday, September 30, 2009 7:45 AM | Feedback (0)
Presenting for the St. Louis .NET User Group – What’s new in VS2010 and .NET 4.0

Last night I had the pleasure of presenting a session on ‘What’s new in VS2010 and .NET 4.0’ for the St. Louis .NET User Group.  Got to meet with lots of people there, so greets go to all .  I had fun with the session; I liked talking about new technology and it’s pretty neat to see everyone and just mingle around with people in general.  Pizza was great (thanks to last night’s sponsor: Quilogy) – I have to also thank Scott Spradlin for allowing me to present to the group.

The session itself has the same presentation slide as my .NET 4.0 session which I presented in St. Louis Day of .NET, which I’ve made available as detailed in this post.  I did make a project that tries to also demonstrate some of the new constructs, which I didn’t have time to do when I first presented it.  So here it is.  It is a simple WinForms application, with 6 buttons, each of which demonstrates:

I hope this may be of use to others.

On a side note, based on the recommendation of speakers at the St. Louis Day of .NET, I’ve created a Twitter account: mulbud.  Hopefully I can contribute to Twitterverse in a positive way .

posted @ Tuesday, September 29, 2009 9:53 AM | Feedback (1)
Linq == Deferred Execution == Always Re-execute

Got bit by a bug caused by me trying to do stuff smartly in Linq.  Gentle reminder that Linq expressions are always recalculated.  The bit of code is the following:

public static void AddKnownTypes(IEnumerable<Type> knownTypes)
{
   // We only want to collect types that are new (hasn't been registered yet)
   IEnumerable<Type> newTypes = knownTypes.Where(t => !_knownTypes.Contains(t));
   _knownTypes.AddRange(newTypes);

   // Fill in a dictionary of these newly added types
   foreach (Type type in newTypes)
   {
      _types[type.Name] = type;
   }
}

Looking at it, the code seems to be fairly benign; the method accepts a list of types, we only wanted the new ones, so filter using the Where extension method, and then add it to our list and also fill in a dictionary of types.

The bug with the above code is that the foreach body will not be entered (regardless of the types contained in knownTypes or _knownTypes).

In Linq, any queries have its execution deferred; in the case above, the newTypes variable actually has the expression as to what needs to be done.  When calling AddRange, we pass in newTypes as the parameter, and the query is executed then and there.

However, in the foreach, newTypes is still the same expression, so when it gets to that foreach line, the expression is re-evaluated again.  Guess what – at that point _knownTypes already contains all the types in newTypes , thus the query will return an empty set.

We always think of using a variable to store a value and that value doesn’t change (unless we change it or some other code change it).  In this case, it’s actually like a delegate call, which will basically execute a method that will return the value.  I’ll be sure to remember this so I don’t get bit by this again.

Edit:

My friend Kevin Grossnicklaus aptly pointed out that Linq operators fall into 2 groups, the ones that have deferred execution and the ones that are not.  For instance Sum, Count and just about any of the Enumerable extension methods that return an actual concrete type (ToList and ToArray are 2 other examples) will actually execute the query then and there.

It's when the method returns an IEnumerable<T> then the query gets deferred; which can lead to some frustrating bugs.  I'll illustrate some of these bugs in future post .

posted @ Wednesday, September 23, 2009 1:50 AM | Feedback (1)
Methods parameters and return values, what types to use?

I’m trying to be a better developer, so I try to make sure I follow best practices as much as I can.  At times when I’m just creating simple methods though, every now and then I get somewhat hung up as to what do I use as types for my input parameters and return values.  You see, it is something that is so basic, we create methods all the time, but most of the time, we don’t pay much attention to it since we’d just like to continue on with the task at hand.  So I’m writing this to remind myself that writing methods, sometime is not as straightforward as it seems…

A colleague was code-reviewing some project files and noticed that we have a same-intentioned method (same name, similar code on what it does but somewhat different input & return values) in 3 different classes.  Of course, the placement of the code itself is erroneous, but let’s attribute that to hectic development and developer’s unfamiliarity of existing classes / methods.  So, this colleague would like to have only 1 of these methods, essentially combining them.  This is what the method looks like:

public static Collection<Piece> GetMembers(EntityCollection<Piece> pieces)
{
   Collection<Piece> result = new Collection<Piece>();

   foreach (Piece piece in pieces)
   {
      if (piece.IsMembers)
      {
         result.Add(piece);
      }
   }

   return result;
}

Now, the method itself is fairly simple; it accepts an EntityCollection<Piece>, (which is a collection type that we have created for our purposes) and then gets all the entities that passes the IsMembers check.  In the other 2 places, the method differs as follows:

public static Collection<object> GetMembers(Piece[] pieceList)
{
    Collection<object> result = new Collection<object>();
    // Same Code here...
}
public static List<Piece> GetMembers(EntityCollection<Piece> pieces)
{
   List<Piece> result = new List<Piece>();
   // Same Code here...
}

As you can see, the difference is in the return values and input parameter types.  The code inside is almost identical.  I can also relate with the developers that made these methods; when we create methods, we usually just take everything at face value.  In most cases when the method was written, the developer has a need for it and the collection that is used by that particular is an EntityCollection, thus easily enough, the method created accepts an EntityCollection as well.  All too often, this is how we write methods, in general, especially simple helper methods.  Similarly, it just so happens, the consuming code at the time just needed a Collection<Piece>, so the method was created returning that same type.

Of course, looking at the above examples, this means the method is only useful if the particular input & output are that same exact type, thus from a junior developer’s perspective, it may seem to be fine to just create variants of this method; especially since .NET allows methods overloading.  Keep in mind that in my scenario, these methods are not even in the same class!  So the task now becomes, how can we collate all these into one nice method that can be reused?  Hopefully it won’t get changed much (if at all) afterwards?

If you’re at all familiar with interfaces, after seeing these methods, the solution is very, very obvious.  The solution should be something like the following:

public static IList<Piece> GetMembers(IEnumerable<Piece> pieces)
{
   Collection<Piece> result = new Collection<Piece>();

   foreach (Piece piece in pieces)
   {
      if (piece.IsMembers)
      {
         result.Add(piece);
      }
   }

   return result;
}

The input parameter is now of type IEnumerable<Piece> and the return value is of type IList<Piece>.  It is very, very obvious; but how did we get here?  By looking at the disparate methods in existence, we (somewhat intuitively) know that by using interfaces, we can condense the various methods into one.  Careful readers would realize that it breaks consumers of the second variant, since the second variant returns a Collection<object> but returning that as a type is bad practice anyway since the object we’re processing will always be Piece.

The question then would be, how can we get to create this method without first going through the types of consumers that is going to call it?  Over time, I created some generalization rules which in hindsight seem to work fairly well.  The rules for types in methods are as follows:

  • Input parameters types should be of the most BASIC type that is needed by the code within the method; an interface whenever possible
  • Return values should be of the most ADVANCED type possible; an interface whenever possible.

The idea behind the 2 rules are fairly simple; in the example the method code only needs to go through each item in the passed in collection and as such IEnumerable<T> fits perfectly.  The code would have worked with IList<T> or ICollection<T>, but since no other methods are needed from those interfaces, between the 3 candidates we’ll choose the most BASIC type.  By using the most basic type, if a consumer happens to only have access to an IEnumerable<Piece> (maybe they get it from some LINQ manipulations) this method can still be called.

For the return value, the reasoning is a bit more complex: the method can just return a type of Collection<Piece> and that can be made to work with any consuming code.  After all Collection<T> implements IList<T>, ICollection<T>, and IEnumerable<T> and their non-generic variants.  However, if we choose to return an actual concrete type we may possibly have trouble in the future if we needed to return something else. 

One recent example of changing return values that I had encountered was with the advent of WPF; WPF has great support for data binding – but to make sure 2-way data binding works entities have to implement INotifyPropertyChanged and collections have to implement INotifyCollectionChanged.  So we ended up having to change most of our methods that returns a List<T> to return ObservableCollection<T>.  The methods that returned List<T>, we have to change the consumer code to store the return value as IList<T> to make it work.

So that was one lesson that I took to heart, so I try to make public methods (and even private methods as much as possible) to have interfaces are turn values.  I hope that answers the question as to why I chose to return an interface versus returning an actual concrete class. 

The secondary explanation that is required then is why did I chose to use an IList<T>, rather than an IEnumerable<T>?  With interfaces, I have the chose between IList<T>, ICollection<T>, and IEnumerable<T>.  The rule I have is that return values should be of the most ADVANCED type as possible, so I chose IList<T>.  Why?  That’s because in the case we have here, the collection is generated and returned – it’s not being kept, it doesn’t affect any state and in essence the returned value can be used directly by the consuming code.  With that in mind, having the most advanced type returned allows consumers to immediately use the return value.  If the caller wanted to remove the last entry in the list, since we’re returning an IList<T>, the caller can do it using the returned value.  If we had chosen to return an IEnumerable<T>, then the caller have to create its own list and then do the removal process.  So the benefit is for the consuming code, since they can act on the return value better given the more advanced type being returned.

Following my generalized rules, if the Piece entity implements an interface (say IPiece), I would change the method to return IList<IPiece> and accepts IEnumerable<IPiece>.  However, this usually means most of your code should operate on the IPiece interface and rarely do so on the actual concrete Piece class itself.

Every now and then when I create methods, I go through this same thought process.  I hope to make this more seamless for me in the future, so I will automatically always generate methods that are easy to consume.  Hopefully this can be of use to other people.

posted @ Friday, September 18, 2009 7:03 AM | Feedback (2)
.NET 4.0 and C# 4.0 sessions materials

Yes, I know I'm late.  Please forgive me .

In any case, the .NET 4.0 and C# 4.0 sessions materials are now available.  You can download them from here and here, respectively.

In hindsight, the .NET 4.0 presentation should've been called: What's new in .NET 4.0; and even then given the vastness of the material available, it should've been split into 2 sessions: What's new in Visual Studio 2010 and What's new in .NET 4.0 Framework.  Given the 75 minutes available for the presentation, I decided to focus on the VS2010 bits just because when it's out, it is more likely that people will use it as a development tool (it can target .NET 2.0 and on up), but fewer developers can actually use the 4.0-specific bits (dynamic keyword, new WPF controls, etc.) in their work projects.

The .NET 4.0 material is basically the PowerPoint slides, just because it is more of a show-and-tell and as such really goes well with also running VS2010 and demoing the functionalities.  Even with the new language features and also new constructs in .NET 4.0, you can only show it with VS2010, so it is rather ineffective without an actual show of VS2010 itself.

The C# 4.0 material has a couple of projects to demonstrate the new constructs; comparatively to its 2.0 and 3.0 upgrades, the 4.0 upgrade doesn't really bring too much to the table - it is more of a refinement of what is a fine programming language.  Lastly, quite a bit of the slides in this particular session is taken from Anders Hejlsberg's very excellent C# 4.0 presentation in PDC 2008 (the video of which is available here - highly recommend watching this one).  What Anders show what can be done with C# 4.0 goes beyond what I can show with the tools I have available, so for all intents and purposes please do watch his presentation.

I hope this can be of use to others, as it has provided me with hours of learning, even if I don't use it immediately it gives me an awareness of what lurks around the corner in .NET technology coming out from Microsoft.

posted @ Friday, September 04, 2009 5:09 PM | Feedback (1)
Intermediate WPF session material

One of the sessions I did in St. Louis Day of .NET is about Intermediate WPF.  It seems to be one that people seem to have liked, so I’m making the materials available here first for those that may be interested in playing around with it.

The overview description was made before the material is done, and I was not able to show 3D graphics within the sample; my apologies.  In any case, there’s really no text out there that defines what is Intermediate WPF; so I just created one . The session goes through Data Binding, Data Template, Triggers, Styling, Resources, a bit of Animation, just a sliver of Control Templating and lastly Multimedia playback (thru the MediaElement control).

In the past, I was able to upload files onto GeeksWithBlogs and then provide a link to those files for download.  However, something seems to have changed – some of those links will now take the browser to another blog page.  Since I have no control over that, I’m trying to figure out where to host my files.  I don’t expect a lot of people to download any files I put up, so bandwidth should be fairly low; and for now I’m going to put it up on Google Sites.  I already have a Gmail account anyway, so it just seems natural.

Hopefully it would be fine; we’ll see .  If anyone has other suggestion as to a place to drop files in and I can refer through a URL, please let me know.

In any case, without further ado, the session material can be downloaded here.  If anyone’s having trouble downloading the file, please let me know.  The idea for the application itself came from this Styling and Templating sample by Microsoft.

The slides are not very helpful, other than detailing what the session will cover.  I’ve included a document that explains what each project does and also the step-by-step changes made from the starting project to get to the ending project.

The only gotcha (thanks to this blog) is to have the MediaElement WPF control to play the media files (be it MP3, WMV, or AVI), you need to have Windows Media Player 11 installed.  Since my development and presentation platform was Windows 7, everything worked immediately.  It wasn’t until I was trying to clean up the code on my XP machine did I have trouble with the media not playing (I didn’t upgrade my Media Player on my XP machine).  Upgrading WMP fixes it.

Hopefully this can be of use to others; and I’ll try to put up the other 2 sessions by tomorrow.  Have a good day everyone.

posted @ Thursday, September 03, 2009 8:22 AM | Feedback (0)
St. Louis Day of .NET ‘09

This past weekend was pretty awesome; the second annual St. Louis Day of .NET conference went underway. 2 DAYS!  Can you believe it?  It is 2 DAYS of (almost) all-you-can-eat technical sessions; and all for a minuscule $100 (or $125 for those late registrations).

First off, some disclaimer: my comment on STLDODN ‘09 will be biased.  Why?  Because I participated in it and I’m rather involved in it, so of course I can’t quite badmouth it :).  Organizers for the St. Louis Day of .NET 2009 are: Kevin Grossnicklaus, Jeff Fattic, Scott Spradlin, Clint Edmonson, Chris Deweese, Michelle Marcus, Jeff Strauss and yours truly.

In any case, the event as a whole was a BLAST!  Registration was a tad hairy in the morning – sessions start at 7:30 AM, so by 7 AM, we have a long line of people queuing for their registration badges and goodies.  Everyone was cooperative even with some of the hiccups we’re getting; but we could’ve done better.  However, after the not-so-smooth start, everything just came together! Sessions started, all speakers went to bat!

Just to give some insight into speaker preparation as evidenced in these photos here and here, some speakers practice what someone termed “JIT presentation” – I myself am guilty with still tweaking & researching materials within 5 minutes before my session started.

The St. Louis Day of .NET Website has been updated with a link to photos uploaded to the Flickr STLDODN Group.  If anyone have other pictures to add, join the group & upload the pictures – it’ll be fun for all.

Lunch was OK; was so nervous on the first day, I didn’t even eat lunch – had a restless night sleep the day before as well.  But I learned quite a bit about preparation: there’s no such thing as being overprepared – in fact in most cases, you will always be underprepared.  I kept wishing I have a few more hours to check & re-check some stuff.

Got to brush shoulders with some great speakers; my greets go to just about everyone there: Lee Brandt, Brian Button, Keith Dahlby, Kathy Kellenberger, Ralph Wheaton, Perry Simeroth, Ken Sipe, Ilona Shulman, Matt Bremer, Denny Boynton, Chris Hammond, Cuong Dang, Phillip Japikse and others; apologies for not mentioning them, but it’s rather hard juggling all those names in my head.  Hopefully as I get to know these people better, their names will stick better in my head :).  Unfortunately I went only to a select few sessions due to my “JIT presentation” preparation; got to see Kevin G’s JumpStart: WPF, Ilona’s Data Warehousing session, briefly attended Clint’s “How To Start Your Own Business” (this one needed a much bigger room) and Perry’s “Refactoring” session.

I did 4 sessions altogether on 3 different materials: .NET 4.0 (covers new stuff in Visual Studio 2010, C# 4.0, VB.NET 2010, some of the Visual C++ new tidbits and all other grab bag stuff), C# 4.0 and Intermediate WPF, following Kevin G’s JumpStart: WPF session.  The prep was rather tiring, but it feels good to be able to share knowledge with attendees and speakers alike.  I just hope that what I cover can be useful for some people.

I’m prepping my session materials for upload and I’ll try to make it all available this weekend – so my apologies for those that may be waiting for the materials to come out earlier.  The material itself is not difficult, it’s just that I need to go over them and make sure comments and variables names are proper, and also put in a text file / document to accompany the slides / application.

And believe it or not… planning for St. Louis Day of .NET 2010 is already underway.  Link to attendee survey was sent out last night; if you get them please be honest with your feedback so we can try to address your concerns and make the next conference be even better!

posted @ Tuesday, September 01, 2009 8:57 AM | Feedback (1)
News