What is the maximum length of a C#/CLI identifier?

David Schmitt picture David Schmitt · Oct 9, 2008 · Viewed 17.5k times · Source

Which other restrictions are there on names (beside the obvious uniqueness within a scope)?

Where are those defined?

Answer

qwertium picture qwertium · Jan 9, 2012

In addition to the other answers, the maximum identifier length that is accepted by the Microsoft Visual C# compiler is 511 characters. This can be tested with the following code:

class Program
{
    private static void Main(string[] args)
    {
        int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 5;
    }
}

The length of the variable name there is 511 characters. This code compiles, but if one character is added to the name, the compiler outputs error CS0645: Identifier too long.