views:

725

answers:

3

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

Where are those defined?

+3  A: 

From the PDF of ECMA-335, Partition II, section 22, "Metadata preserves name strings, as created by a compiler or code generator, unchanged. Essentially, it treats each string as an opaque blob. In particular, it preserves case. The CLI imposes no limit on the length of names stored in metadata and subsequently processed by the CLI".

If I've read this correctly and the context is correct then there's no actual limit to the length of an identifier in the CLR.

Rob
so does the .NET c# compiler preserve the names as in the actual source? and wouldn't that mean that using long variable names makes the program less efficient?
fearofawhackplanet
+2  A: 

The C# language specification defines identifiers in section 2.4.2 of the Unified C# 3.0 spec. Basically it's "letter or underscore" followed by any number of "letter, decimal digit, connecting character, combining character, formatting character". To use a keyword as an identifier you need to put @ in front, e.g. int @int = 5;

I haven't looked into the CLI spec, but I know it's slightly less restrictive than the C# spec, because the C# compiler uses "unspeakable" names for things like anonymous methods - these typically include angle brackets, which are valid in the CLI but not valid in C#.

EDIT: There are no explicit name length restrictions in the C# spec, but anything with a double underscore is reserved "for use by the implementation".

Jon Skeet
A: 

Looking in the Partition II Metadata docs, it states that an identifier is either an ID or an SQSTRING

An ID is described as

a contiguous string of characters which starts with either an alphabetic character or one of “_ ”, “$”, “@” or “?” and is followed by any number of alphanumeric characters or any of “_ ”, “$”, “@”, or “?”

Which would imply there's no length limit.

Damien_The_Unbeliever