Movatterモバイル変換


[0]ホーム

URL:


The MothMy Photo - click to enlarge

Developer, Former MVP, now at Microsoft - Best of2004,2005,2006,2007,2008,2009,2010,2011,2012,2013

« WindowsClient | Using Extension methods in Fx 2.0 projec... »

Using C# 3.0 from .NET 2.0

Sun, May 13, 2007, 03:15 PM underOrcas | VisualStudio | LINQ
Please note that this and my next post are based onBeta 1 Orcas bits. Later builds might change the facts so always try it yourselves.

There still appears to be some confusion with regards to the new language features that make LINQ to objects work. In particular, there are claims that it all works with .NET 2.0 and counterclaims that it doesn't. IMO the confusion arises from people not being precise about what they are talking about. I'll try and give you here my view (which of course I think is as clear as can get :-))

To start with, everything here is in the context of Visual Studio "Orcas" – forget earlier versions. Orcas supportsmultitargeting so you can build projects that target .NET Framework v2.0. So the real question we are addressing here is whether you can choose to build a NetFx 2.0 project that also uses some of the new C#3 language features.

If you are not familiar with LINQ, please go read my detailedLINQ blog posts and then come back. So after reading about LINQ I hope you realise that:
LINQ =
1. local variable inference +
2. object intiliazers +
3. anonymous types +
4. lambda expressions +
5. extension methods +
6. query expressions +
7. framework support.

Let's look at some of the language features. The first 4do work with 2.0 projects i.e. the following compiles and runs fine in a 2.0 Orcas project:
class Program
{
delegate T Func<T>(T t);
static void Main(string[] args)
{
// object init -
// makes no sense in this example but hey...
Timer t = new Timer(2000)
{
AutoReset = false,
Enabled = true
};

// local var inf, anonymous types
var i = new { Name = "Fred", Age = 6 };
Console.WriteLine(i.ToString());

// lambdas
Func<int> f = j => j + 1;
Console.WriteLine(f(5));

Console.ReadLine();
}
}
Can you spot them?Local variable type inference,object initiliazers,lambda expressions andanonymous types. This shouldn't surprise you if you read my previous blog posts since I have stated clearly many times that these language features are simply compiler tricks. The IL that gets generated is the same old IL – no dependency on the framework and no dependency on the runtime. So 2.0 projects in Orcas can use 4 of the smart compiler tricks, this is good! As an aside, other 3.0 compiler features irrelevant to LINQ such asautomatic properties, also work in 2.0 projects.

On the gloomy side, it goes without saying that number 7 above, the "framework support", resides inSystem.Core.dll and you cannot reference that assembly from 2.0 projects, so noextension method implementations for you in 2.0 projects. If at this point we think of number 6 above, "query expressions" (i.e.from...where...select syntax), we realise that their usefulness is in friendly mapping to the extension method implementations in System.Core.dll (or System.Xml.Linq or Systam.Data.Linq or your own extension method implementations). So even though query expressions are supported when targeting v2.0 projects from VS "Orcas" (the syntax highlighting when you try it is a clue) there is no implementation to map them onto - other than your own. Well, even your "own implementation" implies that you can use Extension methods for v2.0 projects and I haven't addressed that yet (number 5 above).See my next post now.
Comments [0] |#Permalink

© Copyright 2004-2025, Daniel Moth -Disclaimer
Powered by newtelligence dasBlog 2.3.9074.18820
Page rendered Friday, 18 July 2025 10:32:32 (Pacific Daylight Time, UTC-07:00)


About
Tags
Latest Posts
Archives

[8]ページ先頭

©2009-2025 Movatter.jp