5

I have recently started a new job and am being forced to use VS 2005 and .NET 2. This has been quite painful for a number of reasons, but the one big change from VS 2005 to its successors was the introduction of LINQ and extension methods.

A common pattern that I used to follow when using enumerations was the following:

foreach(var myObject in myObjects.Where(x => x.Property == someValue)
{
    //Do some stuff
}

What I am having difficulty with is coming up with a succinct way of expressing the same statement in C# 2. The best I have come up with is the following:

foreach(MyObject myObject in myObjects)
{
    if(myObject.Property == someValue)
    {
        //Do some stuff
    }
}

Is there a more effective or efficient way of writing this code?

How would you write C# 2 code so that it can most easily be transformed into C# 5 code (we will be upgrading to VS2012/.NET 4.5 after the next release)?

6
  • 1
    Perhaps you should ask your new team if they are not willing to change to VS 2008 (with .NET FW 2 as target platform). The .NET 3.5 compiler works greatly with .NET Fw 2.0, and with Linqbridge you can code as if you had almost full Linq available.
    – Doc Brown
    Commented Sep 19, 2013 at 6:40
  • I would love to move to either VS2010/2012, but I get absolutely zero say in the matter. The boss won't buy it until the next release is out. I have tried explaining how the later compilers will increase productivity greatly, but to no avail. I should add that I'm the sole developer on this project and report straight to the boss.
    – Stephen
    Commented Sep 19, 2013 at 7:45
  • 1
    Visual Studio 2012 Express edition is free, and (unlike previous express versions of VS) allows commercial use.
    – Peregrine
    Commented Sep 19, 2013 at 9:31
  • @Peregrine: previous express editions have been free for commercial use, too.
    – Doc Brown
    Commented Sep 19, 2013 at 11:03
  • 1
    Moving from VS2008 or earlier to VS2012 can be a quite a pain in the *** (and expensive!), esp if you have C++ projects in your solution.
    – Thomas
    Commented Sep 19, 2013 at 14:54

2 Answers 2

10

Well, you have a few interesting bits to overcome:

  • Lambda syntax wasn't introduced until VS 2008, so you'd have to implement using anonymous delegates.
  • You can't even simulate extension methods for .NET 2 with VS 2005. VS 2008 only.
  • Func<> and its relatives weren't introduced until .NET 3.5
  • The Func<> delegates are covariant on their input and contravariant on their output. Not going to have that until .NET 4, so your use may be limited.

That being said, if you grab LINQBridge, you should be able to remove the extension method syntax and use it.

EDIT

So, for grins and giggles, I just translated LINQBridge into .NET 2.0 / VS 2005. Was easy, but not terribly pleasant :) Your example foreach above would wind up looking like this after the re-do:

foreach(MyObject myObject in Enumerable.Where(myObjects, delegate(MyObject x) { return x.Property == someValue; }))
{
    //Do some stuff
}

This makes me think that your non-LINQ foreach is much easier to read for the environment you're working in.

3
  • 4
    +1 for non-LINQ foreach is much easier to read
    – Fabio
    Commented Sep 19, 2013 at 5:49
  • 4
    "Lambda syntax wasn't introduced until VS 2008" etc. immediately made me think of "there won't be a gas station around until sometime next century!" (sorry, I couldn't resist :) ) Commented Sep 19, 2013 at 7:02
  • 2
    There is always win in tapping in a Back to the Future III quote! Commented Sep 19, 2013 at 12:29
4

Before LINQ has been introduced we used PowerCollections (https://powercollections.codeplex.com/). To give you some examples:

Algorithms.Convert(enumerable, converter) <=> enumerable.Select(converter)
Algorithms.Exists(enumerable, predicate) <=> enumerable.Any(predicate)
Algorithms.FindFirstWhere(enumerable, predicate) <=> enumerable.FirstOrDefault(predicate)

and so on. Unfortunately, you will have to use this clunky "delegate (...) { ... }" syntax instead of lambda syntax, but technically it's the same thing.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.