How to Compile C# with Specific Language Version

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(); } }

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

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.

* 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.

--------------Solutions-------------

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.

The documentation states that:

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

and

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.

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

Category:c# Time:2017-12-02 Views:2
Tags: csc

Related post

  • How to compile library with source code with NDK tools? 2011-06-18

    How to compile library with source code ? I am developing the native library with android ndk. Sometimes I got the crash dump messages from logcat. 06-18 15:24:58.545: INFO/DEBUG(24667): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

  • Sitecore restrict access of a role to a specific language version of a page 2012-02-16

    Is it possible to restrict access for a given Sitecore editor role to a specific language version of an item in Sitecore? I am considering a site which would be multi-lingual where there is the requirement to allow editors in the appropriate country

  • How to rename files with specific extension using pig or hadoop fs option? 2014-02-20

    I have *.gz files in mm/dd/yyyy partition based folder structure . I want to rename files which ends with .gz extension. Please suggest how to rename files with specific extension using hadoop fs command line option or using pig. here is my folder st

  • How to Enforce C++ compiler to use specific CRT version? 2009-04-08

    I am using VS2008 for developing a COM dll which by default uses CRT version 9 but I am using TSF (Text service framework) that is not compatible with the new CRT. I think the solution is to use the compatible one so how can I specify the CRT version

  • how to compile MPI and non-MPI version of the same program with automake? 2010-10-19

    I have a C++ code that can be compiled with MPI support depending on a certain preprocessor flag; missing the appropriate flag, the sources compile to a non-parallel version. I would like to setup the Makefile.am so that it compiles both the MPI-para

  • Compiling vim with specific version of Python 2011-05-03

    I'm working on several Python projects who run on various versions of Python. I'm hoping to set up my vim environment to use ropevim, pyflakes, and pylint but I've run into some issues caused by using a single vim (compiled for a specific version of

  • How to compile SecureUDID with CodeRunner 2012-04-21

    I'm trying to quickly test what kind of results I'd get using SecureUDID before I can use in my Xcode project. In this case I'm using CodeRunner but I've never compiled obj-c code outside of Xcode. I've attached the error I'm getting when using the d

  • How can I start a specific excel version in com automation? 2009-01-18

    I use Excel via COM automation (in c#), but it seems that the problem has no control over what version of excel is started on the box - we use both Excel 9 and Excel 11 and one specific set of spreadsheets requires excel 9 else they wont work. I incl

  • How to study programming with C language 2010-03-22

    I am using only C for 5 years. So I am sure that I know C grammar, but I have no idea how to advance programming skills. There are many books for modern languages (such as C++, Java) to study programming skills like the refactoring or pattern, softwa

  • Run test in phpunit with specific php version 2014-07-10

    I have installed multiple PHP versions on my Mac and want to run unit-tests against a specific PHP version (or against multipls versions) Here's the php versions I have: $ php --version Output: PHP 5.4.23 ... $ /Applications/MAMP/bin/php/php5.2.17/bi

  • How to compile OpenCV with ICC? 2009-08-13

    I am trying to compile OpenCV with icc on Linux, in order to profile the execution with intel tools. I installed the last version of icc with default options. I tried both "user" (icc is installed in my home) and "sudo" (icc is intalled in /opt) inst

  • How to link to a specific framework version in Xcode? 2010-03-10

    I've got an application that links against OS X's Python.framework. Since Snow Leopard has upgraded to Python 2.6, the framework now contains versions for 2.4, 2.5, and 2.6. My program doesn't seem to want to link against 2.6, though, and this ends u

  • how to select elements with specific id without using javascript frameworks? 2010-10-04

    I am trying to find the list of elements that matches the specified id format. like I want to select all the tags with classname "required" or select all the tags with id like myObj[any char] eg. myObj1, myObj2, myObj3, myObja etc --------------Solut

  • How to compile sqlite with ICU? 2011-07-05

    I downloaded sqlite from http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz How can I compile sqlite with icu ? --------------Solutions------------- 1) You can compile it as dynamic extension of SQLite Citing http://www.sqlite.org/cvstrac/fileview?

  • How to retrieve string with different language from Edittext 2011-09-20

    I faced this problem how can I retrieve string with another language (not English) from edittext? --------------Solutions------------- You should be able to simply call toString() in your TextWatcher: Set your listener: name.addTextChangedListener(ne

  • how to compile php with the --with-readline option 2011-12-13

    I recently asked this question about a PHP interactive shell, similar to python's. after viewing this page it looks like what I need to do is compile PHP with the --with-readline option. The problem is, on Windows, I don't know how to do that. Can an

  • How to hide elements with specific attribute values with Jquery 2013-05-13

    I have a list like this <ul> <li language="English"> Australia </li> <li language="English"> America </li> <li language="French"> France </li> <li language="French"> Canada </li> <li langauge="

  • C# How to declare variables with specific index? 2013-09-08

    This question already has an answer here: How do I name variables dynamically in C#? 8 answers Generally, we declare variable with specific name. int x=1; int life2 = 100; But what if i want to declare group of variable with some specific index like

  • How to view webpages with different browser versions in firefox and chrome? 2011-09-19

    In the latest version of internet explorer if you press F12 you can bring up a menu that allows you to view the webpage with a different version of IE. Is there an equivalent functionality for this in Firefox and Chrome? --------------Solutions------

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.845 (s). 13 q(s)