是使用关键字var在C#2.0坏? [英] Is using the keyword var bad in C# 2.0?

查看:221
本文介绍了是使用关键字var在C#2.0坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读<一的文章href="http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-$c$c.aspx">using C#在C#3设有2 在这里你可以为实例类型变种X = 2; 键,即使该项目是一个2.0的项目,在Visual Studio 2008编译器精选它并产生相同的code,如果你键入 INT X = 2

I read an article about using C# 3 features in C# 2 where you can for instance type var x = 2; and even if the project is a 2.0 project, the Visual Studio 2008 compiler picks it up and generates the same code as it would if you type int x = 2.

但我不明白的是,你应该的没有的做到这一点在某些情况下?我一直以为 VAR 关键字没有到达,直到C#3。如果编译器生成相同的code,我可以键入C#3 code和C#2 code一模一样,什么是延异真的,因为CLI是一样的,对吧?

But what I don't get is, should you not do this in some cases? I always thought that the var keyword didn't arrive until C# 3.. If the compiler generates the same code and I can type C# 3 code and C# 2 code exactly the same, what is the differance really, because the CLI is the same, right?

从链接引用上述

在幕后,编译器生成常规.NET 2.0 code。

Behind the scenes, the compiler generate regular .NET 2.0 code.

有没有之间的.NET 2.0 code和.NET 3 code?有什么区别

Is there any difference between .NET 2.0 code and .NET 3 code?

推荐答案

我怕你是混合了C#版本和.NET版本。

I am afraid you are mixing up C# versions and .NET versions.

您不能使用在C#2.0 VAR ,编译器会觉得这只是一个标识符。但是你可以使用它在C#3.0面向.NET 2.0,因为 VAR 只是一个语言(C#)构建,而不是.NET结构。在.NET编译器将它翻译成合适的类型中生成的CIL,所以JIT编译器永远不会看到它,你就会完全没问题。

You cannot use var in C# 2.0, the compiler will think it is just an identifier. But you can use it in C# 3.0 targeting .NET 2.0, since var is just a language (C#) construct, not a .NET construct. The .NET compiler will translate it into the appropriate type in the generated CIL, so the JIT compiler will never see it and you will be completely fine.

由于VS2008的编译器是一个C#3.0编译器,你会使用没有问题 VAR 在那里,无论.NET版本,你的目标。

Since the VS2008 compiler is a C# 3.0 compiler, you will have no problem using var there, regardless of the .NET version you are targeting.

这篇关于是使用关键字var在C#2.0坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆