简体   繁体   English

C#/ CLI标识符的最大长度是多少?

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

Which other restrictions are there on names (beside the obvious uniqueness within a scope)? 名称上还有哪些其他限制(除了范围内明显的唯一性之外)?

Where are those defined? 这些在哪里定义?

In addition to the other answers, the maximum identifier length that is accepted by the Microsoft Visual C# compiler is 511 characters . 除其他答案外,Microsoft Visual C#编译器接受的最大标识符长度为511个字符 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. 变量名称的长度有511个字符。 This code compiles, but if one character is added to the name, the compiler outputs error CS0645: Identifier too long . 该代码可以编译,但是如果在名称中添加一个字符,则编译器将输出error CS0645: Identifier too long

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". ECMA-335PDF中 ,第II部分,第22节,“元数据保留由编译器或代码生成器创建的名称字符串,不变。本质上,它将每个字符串视为不透明的blob。特别是,它保留大小写。 CLI对存储在元数据中并随后由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. 如果我已正确阅读且上下文正确,则对CLR中标识符的长度没有实际限制。

The C# language specification defines identifiers in section 2.4.2 of the Unified C# 3.0 spec. C#语言规范在Unified C#3.0规范的2.4.2节中定义标识符。 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, eg int @int = 5; 要将关键字用作标识符,您需要在@前面加上@,例如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#. 我没有研究过CLI规范,但我知道它的限制比C#规范要少一些,因为C#编译器对诸如匿名方法之类的东西使用“难以置信”的名称-这些名称通常包括尖括号,这些括号在CLI中有效,但是在C#中无效。

EDIT: There are no explicit name length restrictions in the C# spec, but section 2.4.2 does have one reservation: 编辑:C#规范中没有明确的名称长度限制,但是2.4.2节确实有一个保留:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. 包含两个连续下划线字符(U + 005F)的标识符保留给实现使用。 For example, an implementation might provide extended keywords that begin with two underscores. 例如,一个实现可能提供以两个下划线开头的扩展关键字。

In other words, it's implementation-specific as to whether a particular identifier beginning with __ will work. 换句话说,特定于实现的是关于以__开头的特定标识符是否有效。 (Some compilers may have __foo as an extended keyword, and some may not.) (某些编译器可能将__foo作为扩展关键字,而有些则可能没有。)

According to various places in "Expert .NET 2.0 IL Assembler" (Serge Lidin, Apress), the functional CIL/CLR limit is 1,023 bytes as encoded in UTF-8. 根据“ Expert .NET 2.0 IL汇编器”(Serge Lidin,Apress)中的不同地方,功能性CIL / CLR限制为1023字节(以UTF-8编码)。 Quotes: 语录:

Page 122: 第122页:

The length of either kind of name in ILAsm is not limited syntactically, but metadata rules impose certain limitations on the name length. ILAsm中任何一种名称的长度都没有语法限制,但是元数据规则对名称长度施加了某些限制。

Page 126: 第126页

The common language runtime imposes a limitation on the full class name length, specifying that it should not exceed 1,023 bytes in UTF-8 encoding. 公共语言运行时对类名的完整长度施加了限制,并指定其以UTF-8编码不得超过1,023字节。 The ILAsm compiler, however, does not enforce this limitation. 但是,ILAsm编译器不强制执行此限制。 Single quotes, should they be used for simple names in ILAsm, are a purely lexical tool and don't make it to the metadata; 如果单引号用于ILAsm中的简单名称,则它们是纯词汇工具,并且不用于元数据。 thus, they don't contribute to the total length of the full class name. 因此,它们不会增加全类名的总长度。

Page 143: 第143页

[TypeDef Table] The combined length of the strings referenced by the Name and Namespace entries must not exceed 1,023 bytes. [TypeDef表]名称和命名空间条目引用的字符串的组合长度不得超过1,023字节。

Page 144: 第144页

[TypeRef Table] [run time] The combined length of the strings referenced by the Name and Namespace entries must not exceed 1,023 bytes. [TypeRef表] [运行时]名称和命名空间条目引用的字符串的组合长度不得超过1,023字节。

Page 186: 第186页

[Method Table] Name (offset in the #Strings stream). [方法表]名称(#Strings流中的偏移量)。 The name of the method (not including the name of the class to which the method belongs). 方法的名称(不包括该方法所属的类的名称)。 This entry must index a string of nonzero length no longer than 1,023 bytes in UTF-8 encoding. 该条目必须以UTF-8编码为长度不超过1,023字节的非零长度的字符串编制索引。

Looking in the Partition II Metadata docs, it states that an identifier is either an ID or an SQSTRING 在Partition II Metadata文档中查看,它指出标识符是ID还是SQSTRING

An ID is described as ID描述为

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. 这意味着没有长度限制。

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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAAAAAZZZ
{            
}

If you would add 1 more character to it, it doest not compile and surprisingly the error list is empty too! 如果要再添加1个字符,它不会编译,并且令人惊讶的是错误列表也为空!

Edit: For my colleague with VS2013 Update 3 the class name limit = 1012 . 编辑:对于我与VS2013 Update 3的同事,类名限制= 1012 Dont know the reason why. 不知道为什么的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM