Updated C# all-in-one file


Thanks to everyone who came up with suggestions on my Did I miss any C# syntax construct? post.

The updated file is below.

#error Error message
#warning Warning message
#pragma warning disable 414, 3021
#pragma warning restore 3021
#line 6
#line 2 "test.cs"
#line default
#line hidden
#define foo
#if foo
#else
#endif
#undef foo

extern alias Foo;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using M = System.Math;

#if DEBUG || TRACE
using System.Diagnostics;
#elif SILVERLIGHT
using System.Diagnostics;
#else
using System.Diagnostics;
#endif

#region Region

#region more
using ConsoleApplication2.Test;
#endregion
using X = int1;
using X = ABC.X<int>;

#endregion

[assembly: System.Copyright(@"(C) 2009")]
[module: System.Copyright("\n\t\u0123(C) 2009" + "\u0123")]

class TopLevelType : IDisposable
{
    void IDisposable.Dispose() { }
}

namespace My
{
    using A.B;

    interface CoContra<out T, in K> { }
    delegate void CoContra2<out T, in K> () where T : struct;

    public unsafe partial class A : C, I
    {
        [method: Obsolete]
        public A([param: Obsolete] int foo) :
            base(1)
        {
        L:
            {
                int i = sizeof(int);
                ++i;
            }

#if DEBUG
      Console.WriteLine(export.iefSupplied.command);
#endif
            const int? local = int.MaxValue;
            const Guid? local0 = new Guid(r.ToString());

            var привет = local;
            var мир = local;
            var local3 = 0, local4 = 1;
            local3 = local4 = 1;
            var local5 = null as Action ?? null;
            var local6 = local5 is Action;

            var u = 1u;
            var U = 1U;
            long hex = 0xBADC0DE, Hex = 0XDEADBEEF, l = -1L, L = 1L, l2 = 2l;
            ulong ul = 1ul, Ul = 1Ul, uL = 1uL, UL = 1UL,
                lu = 1lu, Lu = 1Lu, lU = 1lU, LU = 1LU;

            bool @bool;
            byte @byte;
            char @char = 'c', \u0066 = '\u0066', hexchar = '\x0130', hexchar2 = (char)0xBAD;
            string \U00000065 = "\U00000065";
            decimal @decimal = 1.44M;
            dynamic @dynamic;
            double @double = M.PI;
            float @float = 1.2f;
            int @int = local ?? -1;
            long @long;
            object @object;
            sbyte @sbyte;
            short @short;
            string @string = @"""/*";
            uint @uint;
            ulong @ulong;
            ushort @ushort;
           
            dynamic dynamic = local5;
            var add = 0;
            var ascending = 0;
            var descending = 0;
            var from = 0;
            var get = 0;
            var global = 0;
            var group = 0;
            var into = 0;
            var join = 0;
            var let = 0;
            var orderby = 0;
            var partial = 0;
            var remove = 0;
            var select = 0;
            var set = 0;
            var value = 0;
            var var = 0;
            var where = 0;
            var yield = 0;

            if (i > 0)
            {
                return;
            }
            else if (i == 0)
            {
                throw new Exception();
            }
            var o1 = new MyObject();
            var o2 = new MyObject(var);
            var o3 = new MyObject { A = i };
            var o4 = new MyObject(@dynamic)
            {
                A = 0,
                B = 0,
                C = 0
            };
            var o5 = new { A = 0 };
            var dictionaryInitializer = new Dictionary<int, string>
            {
                {1, ""},
                {2, "a"}
            };
            float[] a = new float[]
            {
                0f,
                1.1f
            };
            int[] arrayTypeInference = new[] { 0, 1, };
            switch (i)
            {
                case 1:
                    {
                        goto case 2;
                    }
                case 2:
                    {
                        goto default;
                        break;
                    }
                default:
                    {
                        return;
                    }
            }
            while (i < 10)
            {
                ++i;
            }
            do
            {
                ++i;
            }
            while (i < 10);
            for (int j = 0; j < 100; ++j)
            {
                Console.WriteLine(j);
            }
            foreach (var i in Items())
            {
                if (i == 7)
                    return;
                else
                    continue;
            }
            checked
            {
                checked(++i);
            }
            unchecked
            {
                unchecked(++i);
            }
            lock (sync)
                process();
            using (var v = BeginScope())
            using (A a = new A())
            using (BeginScope())
                return;
            yield return this.items[3];
            yield break;
            fixed (int* p = stackalloc int[100])
            {
                *intref = 1;
            }
            unsafe
            {
                int* p = null;
            }
            try
            {
                throw null;
            }
            catch (System.AccessViolationException av)
            {
                throw av;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                try { } catch { }
            }
            var anonymous =
            {
                A = 1,
                B = 2,
                C = 3,
            };
            var query = from c in customers
                        let d = c
                        where d != null
                        join c1 in customers on c1.GetHashCode() equals c.GetHashCode()
                        join c1 in customers on c1.GetHashCode() equals c.GetHashCode() into e
                        group c by c.Country
                            into g
                            orderby g.Count() ascending
                            orderby g.Key descending
                            select new { Country = g.Key, CustCount = g.Count() };
        }
        ~A()
        {
        }
        private readonly int f1;
        [Obsolete]
        [NonExisting]
        [Foo::NonExisting(var, 5)]
        [CLSCompliant(false)]
        [Obsolete, System.NonSerialized, NonSerialized, CLSCompliant(true || false & true)]
        private volatile int f2;
        [return: Obsolete]
        [method: Obsolete]
        public void Handler(object value)
        {
        }
        public int m<T>(T t)
          where T : class, new()
        {
            base.m(t);
            return 1;
        }
        public string P
        {
            get
            {
                return "A";
            }
            set;
        }
        public abstract string P
        {
            get;
        }
        public abstract int this[int index]
        {
            protected internal get;
            internal protected set;
        }
        [method: Obsolete]
        [field: Obsolete]
        [event: Obsolete]
        public readonly event Event E;
        [event: Test]
        public event Action E1
        {
            [Obsolete]
            add { value = value; }
            [Obsolete]
            [return: Obsolete]
            remove { }
        }
        public static A operator +(A first, A second)
        {
            Delegate handler = new Delegate(Handler);
            return first.Add(second);
        }
        [method: Obsolete]
        [return: Obsolete]
        public static bool operator true(A a)
        {
            return true;
        }
        public static bool operator false(A a)
        {
            return false;
        }
        class C
        {
        }
    }
    public struct S : I
    {
        public S()
        {
        }
        private int f1;
        [Obsolete]
        private volatile int f2;
        public abstract int m<T>(T t)
          where T : struct
        {
            return 1;
        }
        public string P
        {
            get
            {
                int value = 0;
                return "A";
            }
            set;
        }
        public abstract string P
        {
            get;
        }
        public abstract int this[int index]
        {
            get;
            internal protected set;
        }
        public event Event E;
        public static A operator +(A first, A second)
        {
            return first.Add(second);
        }
        fixed int field[10];
        class C
        {
        }
    }
    public interface I
    {
        void A(int value);
        string Value
        {
            get;
            set;
        }
    }
    [type: Flags]
    public enum E
    {
        A,
        B = A,
        C = 2 + A,

#if DEBUG
    D,
#endif

    }
    public delegate void Delegate(object P);
    namespace Test
    {
        using System;
        using System.Collections;
        public class Список
        {
            public static IEnumerable Power(int number, int exponent)
            {
                Список Список = new Список();
                Список.Main();
                int counter = 0;
                int אתר = 0;
                while (++counter++ < --exponent--)
                {
                    result = result * number + +number+++++number;
                    yield return result;
                }
            }
            static void Main()
            {
                foreach (int i in Power(2, 8))
                {
                    Console.Write("{0} ", i);
                }
            }
        }
    }
}

namespace ConsoleApplication1
{
    namespace RecursiveGenericBaseType
    {
        class A<T> : B<A<T>, A<T>>
        {
            protected virtual A<T> M() { }
            protected abstract B<A<T>, A<T>> N() { }
            static B<A<T>, A<T>> O() { }
        }

        sealed class B<T1, T2> : A<B<T1, T2>>
        {
            protected override A<T> M() { }
            protected sealed override B<A<T>, A<T>> N() { }
            new static A<T> O() { }
        }
    }

    namespace Boo
    {
        public class Bar<T> where T : IComparable
        {
            public T f;
            public class Foo<U> : IEnumerable<T>
            {
                public void Method<K, V>(K k, T t, U u)
                    where K : IList<V>, IList<T>, IList<U>
                    where V : IList<K>
                {
                    A<int> a;
                }
            }
        }
    }

    class Test
    {
        void Bar3()
        {
            var x = new Boo.Bar<int>.Foo<object>();
            x.Method<string, string>(" ", 5, new object());

            var q = from i in new int[] { 1, 2, 3, 4 }
                    where i > 5
                    select i;
        }

        public static implicit operator Test(string s)
        {
            return new ConsoleApplication1.Test();
        }
        public static explicit operator Test(string s)
        {
            return new Test();
        }

        public int foo = 5;
        void Bar2()
        {
            foo = 6;
            this.Foo = 5.GetType(); Test t = "sss";
        }

        public event EventHandler MyEvent = delegate { };

        void Blah()
        {
            int i = 5;
            int? j = 6;

            Expression<Func<int>> e = () => i;
            Expression<Func<bool, Action>> e2 = b => () => { return; };
            Func<bool, bool> f = delegate (bool a)
            {
                return !a;
            };
            Action a = Blah;
        }

        public Type Foo
        {
            [Obsolete("Name", error = false)]
            get
            {
                return typeof(IEnumerable<>);
            }
            set
            {
                var t = typeof(System.Int32);
                t.ToString();
                t = value;
            }
        }

        public void Constants()
        {
            int i = 1 + 2 + 3 + 5;
            global::System.String s = "a" + (System.String)"a" + "a" + "a" + "a" + "A";
        }

        public void ConstructedType()
        {
            List<int> i = null;
            int c = i.Count;
        }
    }
}

namespace Comments.XmlComments.UndocumentedKeywords
{
    /// <summary>
    /// Whatever
    /// </summary>
    /// <!-- c -->
    /// <![CDATA[c]]> //
    /// <c></c> /* */
    /// <code></code>
    /// <example></example>
    /// <exception cref="bla"></exception>
    /// <include file='' path='[@name=""]'/>
    /// <permission cref=" "></permission>
    /// <remarks></remarks>
    /// <see cref=""/>
    /// <seealso cref=" "/>
    /// <value></value>
    /// <typeparam name="T"></typeparam>
    class /*///*/C<T>
    {
        void M<U>(T t, U u)
        {
            // comment
            /* *** / */
            /* //
             */
            /*s*///comment
            // /***/
            /*s*/int /*s*/intValue = 0;
            intValue = intValue /*s*/+ 1;
            string strValue = /*s*/"hello";
            /*s*/MyClass c = new MyClass();
            string verbatimStr = /*s*/@"\\\\";
        }
    }

    //General Test F. Type a very long class name, verify colorization happens correctly only upto the correct size (118324)
    class TestClassXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/*Scen8*/{ }

    class TestClassXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX22/*Scen9*/{ }

    class yield
    {
        void Foo<U>(__arglist)
        {
            C<U> c = null;
            c.M<int>(5, default(U));
            TypedReference tr = __makeref(c);
            Type t = __reftype(tr);
            int j = __refvalue(tr, int);
            Params(a: t, b: t);
        }
        void Params(ref dynamic a, out dynamic b, params dynamic[] c) {}
        void Params(out dynamic a = 2, ref dynamic c = default(dynamic), params dynamic[][] c) {}

        public override string ToString() { return base.ToString(); }

        public partial void OnError();

        public partial void method()
        {
            int?[] a = new int?[5];/*[] bug*/ // YES []
            int[] var = { 1, 2, 3, 4, 5 };/*,;*/
            int i = a[i];/*[]*/
            Foo<T> f = new Foo<int>();/*<> ()*/
            f.method();/*().*/
            i = i + i - i * i / i % i & i | i ^ i;/*+ - * / % & | ^*/
            bool b = true & false | true ^ false;/*& | ^*/
            b = !b;/*!*/
            i = ~i;/*~i*/
            b = i < i && i > i;/*< && >*/
            int? ii = 5;/*? bug*/ // NO ?
            int f = true ? 1 : 0;/*? :*/   // YES :
            i++;/*++*/
            i--;/*--*/
            b = true && false || true;/*&& ||*/
            i << 5;/*<<*/
            i >> 5;/*>>*/
            b = i == i && i != i && i <= i && i >= i;/*= == && != <= >=*/
            i += 5.0;/*+=*/
            i -= i;/*-=*/
            i *= i;/**=*/
            i /= i;/*/=*/
            i %= i;/*%=*/
            i &= i;/*&=*/
            i |= i;/*|=*/
            i ^= i;/*^=*/
            i <<= i;/*<<=*/
            i >>= i;/*>>=*/
            object s = x => x + 1;/*=>*/
            Point point;
            unsafe
            {
                Point* p = &point;/** &*/
                p->x = 10;/*->*/
            }
            IO::BinaryReader br = null;
        }

        struct Point { public int X; public int Y; }
    }
Comments (22)

  1. Omer Mor says:

    You can use a using and fixed statements with multiple declarators:

    using(var d1 = new D1(), var d2 = new D2())

    {

    }

    fixed(int* p1 = &x, p2 = &y)

    {

    }

  2. Christopher says:

    Multi-parameter lambda:

    (a, b) => …

    Type declarations in the lambda:

    (long x) => x – x

    Empty for loop?

    for(;;) { break; }

  3. Andrey Titov says:

    goto and labels (not inside switch)

    this = something inside struct

    += and -= for delegates and events

  4. Andrey Titov says:

    // this two is special case for parser

    int minInt32Value = -2147483648;

    int minInt64Value = -9223372036854775808L;

  5. You added the typeof(IEnumerable<>) but removed the typeof(IEnumerable<int>)…

  6. styx31 says:

    @Omer Mor doesn’t work for me : error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement

  7. Andrey Titov says:

    typeof(int?) == typeof(Nullable<int>)

    typeof(IEnumerable<int?[][][]>)

    recursive delegates:

    delegate int Recursive(Recursive r, int n);

    delegate R Recursive<A,R>(Recursive<A,R> r, A a);

    http://blogs.msdn.com/wesdyer/archive/2007/02/02/anonymous-recursion-in-c.aspx

    fallback cases in switch

    empty switch (it’s a warning, not an error):

    switch (args.Length)

    {

    }

    compile time constant in case (not just literal):

    switch (i)

    {

       case 2+3:

           Console.WriteLine("5");

           break;

    }

    switch on string/enum value

    multidimensional arrays:

    int[, ,] cube = { { { 111, 112, }, { 121, 122 } }, { { 211, 212 }, { 221, 222 } } };

  8. Andrey Titov says:

    I’m wondering why Console.WriteLine(Nullable<int>.ReferenceEquals(null, null)) works, but Console.WriteLine(int?.ReferenceEquals(null, null)) doesn’t work?

  9. Andrey Titov says:

    [DllImport("kernel32", SetLastError=true)]

    static extern bool CreateDirectory(string name, SecurityAttribute sa);

  10. Andrey Titov says:

    Could you post buildable solution for this file?

  11. Thanks a lot! This file was never intended to build. #error and the 512-char identifier would prevent it. So I’m not worrying about semantic correctness, I just want to make sure I cover as much of the parser I can.

  12. Andrey Titov says:

    var multilineString = @"first line

    second line";

  13. Paul van Brenk says:

    How about

    // context aware keywords out of context

    int yield;

    Some keywords prepended with the @ seem to be missing.

  14. Joren says:

    I think you should include a method call like M(A<B, C>(5)); since the spec has a special rule for resolving the ambiguity. The ambiguity being, is the AST something like (call M (smaller A B) (greater C (5))) or is it (call M (call (generic A (B C)) 5))?

  15. Timwi says:

    Things missing:

    • Multiple variables (not multiple types!) in a using statement, e.g.

               using (IDisposable x = X(), y = Y()) { }

    • Multiple variables and multiple increment statements in a for statement, e.g.

               for (int i = 0, j = 0; i < length; i++, j++) { }

    • Labels on an empty statement, e.g.

               Label: ;

    • Jagged multidimensional arrays, e.g.

               int[][,] arr = new int[5][,]; // as opposed to new int[][5,5]

               arr[0] = new int[5,5];  // as opposed to arr[0,0] = new int[5];

               arr[0][0,0] = 47;

    • Lambda expression with empty body, e.g. ()=>{} or ()=>{;}

    • Explicit implementation of an interface method from a generic interface

    • Multiple case labels in a switch, e.g.

               switch (i) {

                   case 1:

                   case 2:

                       // blah

                       break;

               }

    • Numeric type prefixes:

               var @decimal = 1m;  // you only have capital M

               var @float = 1F;    // you only have lower-case f

               var @double = 1d, @double2 = 1D;

    • Exponential notation:

               double d = -1.2e3;

    • Recursive generic parameter constraints:

               public class Container<T> where T : Container<T> { }

  16. Timwi says:

    Things missing:

    • Multiple variables (not multiple types!) in a using statement, e.g.

               using (IDisposable x = X(), y = Y()) { }

    • Multiple variables and multiple increment statements in a for statement, e.g.

               for (int i = 0, j = 0; i < length; i++, j++) { }

    • Labels on an empty statement, e.g.

               Label: ;

    • Jagged multidimensional arrays, e.g.

               int[][,] arr = new int[5][,]; // as opposed to new int[][5,5]

               arr[0] = new int[5,5];  // as opposed to arr[0,0] = new int[5];

               arr[0][0,0] = 47;

    • Lambda expression with empty body, e.g. ()=>{} or ()=>{;}

    • Explicit implementation of an interface method from a generic interface

    • Multiple case labels in a switch, e.g.

               switch (i) {

                   case 1:

                   case 2:

                       // blah

                       break;

               }

    • Numeric type prefixes:

               var @decimal = 1m;  // you only have capital M

               var @float = 1F;    // you only have lower-case f

               var @double = 1d, @double2 = 1D;

    • Exponential notation:

               double d = -1.2e3;

    • Recursive generic parameter constraints:

               public class Container<T> where T : Container<T> { }

  17. Timwi says:

    Also missing:

    • const fields (you only have const locals)

    • break; in a for/while/do/foreach loop (you only have it in a switch statement)

    • 'out' and 'ref' arguments when calling (you only have out and ref parameter declarations); include both simple variables as well as array elements

  18. Tergiver says:

    Here's an obscure one: Semi-colons are allowed (optionally) after namespace, class, struct, and enum blocks. For example:

    namespace Namespace

    {

      class Class

      {

      };

    };

    You might want to add an escaped quote in a string literal, both @ and not:

    string test1 = @"""", test2 = """;

  19. Ivan Kochurkin says:

    Kirill, what license of your file? Will you plan to update this file with C# 5, 6 syntax?

  20. License is Apache 2.0. Look for the latest file on github.com/…/roslyn

Skip to main content