VS2008 oriented to .NET 2.0 does not prevent me from using C # 3 functions

I had a VS2005 solution and wanted to get out of VS2005 for the new year. Basically, I was pleased with the update process and was pleasantly surprised to see that my build scripts basically still work.

My question is related to the multiple targeting feature - I do not have .NET 3.5 installed on my servers, so I must continue to configure .NET 2.0. It worked mostly, but I found that I could do something like

var returnMe = "result: " + result.ToString(); 

... and still successfully debugging the project.

When I received this code before the build server, the build failed, saying that "var" was not defined.

So what should I expect?

  • VS2008 focused on .NET 2.0 should throw errors when I try to do C # 3 things.
  • The build server, realizing that I am targeting .NET 2.0, must understand what I am doing and compile this thing in a binary compatible binary

Any ideas?

+4
source share
5 answers

The problem is that the build server does not have the correct compiler. You can create it on your workstation, right?

Many of the features of .NET 3.0 are just syntax bits that return to CLR 2 at compile time. var is one of them - when it compiles, the compiler converts var to the appropriate type.

There is a pretty informative post here http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-code.aspx

+5
source

When you use Visual Studio 2008 to target the runtime 2.0, you are still using the 3.0 version of the C # compiler to compile the code. The CLR environment itself has not changed much between 2005 and 2008. In fact, this is just an update package for the main CLR and the addition of several new libraries (in particular, System.Core.dll).

Thus, most of the features added in 2008 work well in runtime 2.0. This is completely legal, and if you try to use a function that does not work in version 2.0, you will encounter an error either at build time or at the time of publication.

+1
source

As others have said, C # 3.0 features can be used when setting up the .NET 2.0 runtime using the C # 3.0 compiler.

A slightly known fact related to this is that you can even use LINQ with .NET 2.0 if you provide your own implementation of LINQ.

Here is an example that allows operators to "select" and "where":

 namespace System.Runtime.CompilerServices { // defining this attribute allows using extension methods with .NET 2.0 [AttributeUsage(AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute {} } namespace System { public delegate R Func<A, R>(A arg0); } namespace System.Linq { public static class Enumerable { public static IEnumerable<R> Select<T, R>(this IEnumerable<T> input, Func<T, R> f) { foreach (T element in input) yield return f(element); } public static IEnumerable<T> Where<T>(this IEnumerable<T> input, Func<T, bool> f) { foreach (T element in input) { if (f(element)) yield return element; } } } 

You can send the Mono version of System.Core with your application if you want to make full use of LINQ on .NET 2.0.

+1
source

If you want to target a specific assembly, you can do this through the configuration settings (this is exactly what I just read for exam 70-536, very dry stuff!).

Add something like the following to the app.exe.config file:

 <configuration> <startup> <requiredRuntime version="v2.0.50727"/> </startup> </configuration> 

Then you can force all your builds to use the same version regardless of the machine, and also help you debug the correct version.

0
source

If you do not use the following libraries / technologies:

  • Windows Presentation Foundation (WPF)
  • Windows Communication Foundation (WCF)
  • Windows Workflow Foundation (WWF)
  • Windows CardSpace (WCS)

then you are more or less safe, although I would recommend rigorous testing anyway.

You can use .Net 3.0 functions such as var , etc. just because VS2008 doesn't care about the target version of the framework when it comes to things like syntax checking on the fly, IntelliSence and maybe others. And as far as I know, VS2010 will pay attention to these things, which will prevent you from successfully Compile your " var x = ... " constructs when targeting version 2.0.

0
source

Source: https://habr.com/ru/post/1299122/


All Articles