The Sharp Sea

I like C#. A far as object-​oriented pro­ce­dur­al lan­guages go, I think this is my favorite. It has a good feel to it and just about every­thing in it seems to fit. Most fea­tures are there to make my life eas­i­er, not the lan­guage design­er’s. This is how it should be. Here are a few of the fea­tures I’m most excit­ed about.

Extension Methods

These rock! Having the abil­i­ty to add func­tion­al­i­ty to class­es you don’t have the code for is awe­some. Don’t get me wrong, there is the poten­tial for mis­use, but once you start to use them you will nev­er look back.

This is much eas­i­er to read and under­stand:

than this:


Generics are good. Generics are great. Where I’m work­ing we still have to use JDK 1.4 so I can’t use gener­ics in Java. Argh, so prim­i­tive. Beyond the usu­al act of mak­ing a gener­i­cized list, when you com­bine gener­ics and exten­sion meth­ods then you have some­thing that real­ly rocks. This is how all of LINQ is built. And because of the way things work you can make your own LINQ oper­a­tors.

But most impor­tant to me is that by mak­ing an exten­sion method gener­ic you can cre­ate one exten­sion that is applic­a­ble to many (if not all) class­es. Trust me, this is real­ly big!

LINQ and Query Expression Syntax

When I first heard about LINQ I thought “OK, yet anoth­er way to embed SQL in a lan­guage.” Boy was I wrong. While you can go against SQL data sources with LINQ, I haven’t yet used LINQ to SQL and I use LINQ all the time. The way that it inte­grates with enu­mer­ables is just amaz­ing.

Given a vari­able People which can be con­vert­ed to IEnumerable which has the appro­pri­ate fields, try to do this with­out LINQ:

This returns an IEnumerable over a list of anony­mous types con­tain­ing three fields: FirstName, LastName, and Age. These items are sort­ed by LastName then FirstName and have been fil­tered to only those who are over 50. Wow!

Partial Classes

I’m not sure when these were added to the lan­guage but these are extreme­ly use­ful. If you have gen­er­at­ed code you can have the gen­er­a­tor gen­er­ate into one file and your hand-​written code go into anoth­er and the com­pil­er will merge them at com­pile time into one class. This gets away from acci­den­tal over­writes com­plete­ly. Or say that you have a bunch of ugly import/​export code that you must have because of the data for­mat you’re sup­port­ing but you real­ly don’t want to see it. Before you would have had to put the code is helper class­es but that means you may have to expose data that you real­ly would rather not. Now you just put the code into a side file which has a bunch of par­tial class­es. The I/​O code has com­plete access because it is part of the class but you don’t have to look at it unless you need to. Brilliant!

Partial Methods

Imagine hav­ing an abstract method that can be imple­ment­ed but does not have to be and (this is the best part) the call to it will not be com­piled into the code if the method is not imple­ment­ed. The pur­pose (I guess) for this is so that auto­mat­i­cal­ly gen­er­at­ed code can gen­er­ate a call to the method and a strong­ly typed def­i­n­i­tion can be cre­at­ed but still leave imple­ment­ing it as option­al. So far that’s all I’ve used it for but it is real­ly help­ful for that case.

Enough Already

Well that’s enough for now. Each of these fea­tures has made using C# much more pleas­ant and pro­duc­tive. Next time I’ll men­tion some Visual Studio fea­tures I like (and maybe a few I don’t) just in time for the VS2010 Beta.

One thought on “The Sharp Sea

  1. Hi, Congratulations to the site own­er for this mar­velous work you’ve done. It has lots of use­ful and inter­est­ing data.

Leave a Reply