如何编译C#与特定语言版本

编程入门 行业动态 更新时间:2024-10-13 00:32:29
本文介绍了如何编译C#与特定语言版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

比方说,我想演示,某人的foreach在C#4.0和5.0之间的差异。

Let's say I want to demo to someone about the differences between foreach in C# 4.0 and 5.0.

所以,我写我的代码片段:

So I write up my code snippet:

public static void Main() { string[] fruits = { "Apple", "Banana", "Cantelope" }; var actions = new List<Action>(); foreach (var fruit in fruits) { actions.Add(() => Console.WriteLine(fruit)); } foreach(var a in actions) { a(); } }

但无论我怎么编译它,它始终工作因为它在5.0 * 。我已经试过了的csproj文件中设置语言版本(建设 - >高级 - >语言版本),我已经试过只是构建它的命令行:

But no matter how I compile it, it always works as it does in 5.0*. I've tried setting the language version in the csproj file (Build -> Advanced -> Language Version) and I've tried just building it on the command line:

csc myProgram.cs /langversion:4

我不能得到它的工作的老的方式。任何帮助吗?奖励积分,如果你能告诉我怎么做它的命令行和Visual Studio上都。

I can't get it to work the "old" way. Any help? Bonus points if you can tell me how to do it on both the command line and Visual Studio.

*对于任何人谁不知道,在C#。 < = 4.0,这将打印 Cantelope Cantelope Cantelope ,而在C#5.0+的那样(更直观地)打印苹果香蕉Cantelope 。 这里有一个链接和的here's另一个。

* For anyone who doesn't know, in C#. <= 4.0 this would print Cantelope Cantelope Cantelope, while in C# 5.0+ it would (more intuitively) print Apple Banana Cantelope. Here's a link, and here's another.

推荐答案

的目的 / langversion 仅使编译器接受特定的语言结构。它不影响编译器的实际行为。

The purpose of the /langversion is only to make the compiler accept specific language constructs. It does not affect the actual behaviour of the compiler.

文档指出:

使编译器只接受包含语法在选择C#语言规范。

Causes the compiler to accept only syntax that is included in the chosen C# language specification.

由于C#编译器的每个版本包含扩展的语言规范,/ langversion不给你的编译器的早期版本的等效功能。

Because each version of the C# compiler contains extensions to the language specification, /langversion does not give you the equivalent functionality of an earlier version of the compiler.

所以要表现出不同的行为,你将不得不使用不同的CSC.EXE,用正确的框架版本安装。

So to demonstrate the different behaviour, you will have to use a different csc.exe, installed with the right framework versions.

C:\Windows\Microsoft.NET\Framework\v3.5>csc /out:c:\temp\foo-35.exe c:\temp\foo.cs Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.7903 for Microsoft (R) .NET Framework version 3.5 Copyright (C) Microsoft Corporation. All rights reserved. C:\Windows\Microsoft.NET\Framework\v3.5>c:\temp\foo-35.exe Cantelope Cantelope Cantelope

更多推荐

如何编译C#与特定语言版本

本文发布于:2023-11-07 00:21:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   版本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!