Search code examples
c#visual-studioecma

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


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.


Solution

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