0

How to force Visual Studio (C#) to compile sources according to ECMA-334 standard?

For example the below code is not valid in ECMA-334 standard:

foreach (var item in custQuery)
{
    Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone);
}

Because var is not a ECMA-334 standard keyword. I want to VS warns me in these situations.

Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
  • Have you looked at something like this? https://visualstudiogallery.msdn.microsoft.com/0099ED9B-8A69-4CB2-85F8-D996A228C158 – ahwm May 28 '15 at 22:51
  • 2
    There is rarely a good reason for this though. ECMA-334 is - by modern programming language standards - ancient. You're missing out on var, extension methods, lambda functions, object initializers, variance, optional parameters, await/async, and a lot more. And that's not counting all the goodies in C#6 – Martijn May 28 '15 at 23:11

2 Answers2

5

You can use the "Language Version" option (aka langversion at the command line). It is on the build tab, under advanced options. You would select ISO-2 for this case.

Note that this is not a backwards compatibility mode. It merely bars you from using features that became available in versions newer than the specified version.

Eric Lippert has a detailed explanation of the use and purpose of the langversion option.

To note how it is not a full backwards compatibility mode, take this example from the article:

class C
{
    public static bool operator < (C c1, C c2) { return true; }
    public static bool operator > (C c1, C c2) { return true; }
    public static bool operator < (bool b1, C c2) { return true; }
    public static bool operator > (bool b1, C c2) { return true; }

    static C H = new C();
    static C I = new C();
    static C J = new C();
    static void G(bool b) { }
    static void Main()
    {
        G ( H < I > ( J ) );
    }
}

The setting langversion to C# 1.0 (ISO-1) bars this as a use of generics, but it was legal in that version of the compiler.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
3

The compilers included with Visual Studio and .NET Framework are not designed to conform to anything other than the latest (at the time of release) C# language version. Although there are options to diagnose uses of >C#-2.0 (ECMA-334) features, through the C# language level setting in your project options, this has no effect on breaking changes in the language that have since been made.

The C# 2.0 language is what's supported by Visual Studio 2005. You can install the freely available Express version of that.