C# .NET 3.0/3.5 features in 2.0 using Visual Studio 2008

前端 未结 7 1952
醉话见心
醉话见心 2020-12-09 05:27

What are some of the new features that can be used in .NET 2.0 that are specific to C# 3.0/3.5 after upgrading to Visual Studio 2008? Also, what are some of the features tha

相关标签:
7条回答
  • 2020-12-09 05:38

    To define extension methods, you'll need to supply the following class if you're targeting .NET 2.0:

    namespace System.Runtime.CompilerServices {
      [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
        sealed class ExtensionAttribute : Attribute { }
    }
    
    0 讨论(0)
  • 2020-12-09 05:38

    You can use Mono's version of the System.Core which fully supports LINQ & Expression Trees. I compiled its source against .net 2.0, and now I can use it in my .net2.0 projects. This is great for projects that needs to be deployed on win2k, where .net3.5 is not available.

    0 讨论(0)
  • 2020-12-09 05:41

    Pretty much everything! Daniel Moth covers this here and here. That only leaves runtime support: LINQ-to-Objects is provided by LINQBridge - which leaves just bigger APIs like Expression support, and tools like LINQ-to-SQL. These are too big to be reasonably ported back to .NET 2.0, so I'd use .NET 3.5 for these.

    0 讨论(0)
  • 2020-12-09 05:45

    There was a previous discussion about something similar you may also want to read too:

    Targeting .NET Framework 3.5, Using .NET 2.0 Runtime. Caveats?

    0 讨论(0)
  • 2020-12-09 05:49

    You can use any new C# 3.0 feature that is handled by the compiler by emitting 2.0-compatible IL and doesn't reference any of the new 3.5 assemblies:

    • Lambdas (used as Func<..>, not Expression<Func<..>> )
    • Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute)
    • Automatic properties
    • Object Initializers
    • Collection Initializers
    • LINQ to Objects (by implementing IEnumerable<T> extension methods, see LinqBridge)
    0 讨论(0)
  • 2020-12-09 05:56

    Lambdas & Extension methods are handled purely by the compiler and can be used with the .Net 2.0 framework.

    0 讨论(0)
提交回复
热议问题