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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/186523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:06:48  来源:igfitidea点击:

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

c#.netclrlanguage-featurescommand-line-interface

提问by David Schmitt

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

名称还有哪些其他限制(除了范围内明显的唯一性)?

Where are those defined?

那些在哪里定义的?

采纳答案by Rob

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 中标识符的长度没有实际限制。

回答by Jon Skeet

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;

C# 语言规范在 Unified C# 3.0 规范的第 2.4.2 节中定义了标识符。基本上它是“字母或下划线”后跟任意数量的“字母、十进制数字、连接字符、组合字符、格式字符”。要将关键字用作标识符,您需要将 @ 放在前面,例如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. For example, an implementation might provide extended keywords that begin with two underscores.

包含两个连续下划线字符 (U+005F) 的标识符保留供实现使用。例如,实现可能提供以两个下划线开头的扩展关键字。

In other words, it's implementation-specific as to whether a particular identifier beginning with __will work. (Some compilers may have __fooas an extended keyword, and some may not.)

换句话说,以 开头的特定标识符是否有效是特定于实现的__。(有些编译器可能有__foo扩展关键字,有些可能没有。)

回答by Damien_The_Unbeliever

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.

这意味着没有长度限制。

回答by qwertium

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:

除了其他答案之外,Microsoft Visual C# 编译器接受的最大标识符长度为511 个字符。这可以使用以下代码进行测试:

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.

变量名的长度有 511 个字符。这段代码可以编译,但如果在名称中添加一个字符,编译器会输出error CS0645: Identifier too long.

回答by Dono

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. Quotes:

根据“Expert .NET 2.0 IL Assembler”(Serge Lidin,Apress)中的不同地方,功能 CIL/CLR 限制为 1,023 字节,以 UTF-8 编码。引号:

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. The ILAsm compiler, however, does not enforce this limitation. Single quotes, should they be used for simple names in ILAsm, are a purely lexical tool and don't make it to the metadata; thus, they don't contribute to the total length of the full class name.

公共语言运行时对完整的类名长度施加了限制,指定它在 UTF-8 编码中不应超过 1,023 个字节。但是,ILAsm 编译器不强制执行此限制。单引号,如果它们用于 ILAsm 中的简单名称,是一种纯粹的词汇工具,不会出现在元数据中;因此,它们不会影响完整类名的总长度。

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 Table] Name 和 Namespace 条目引用的字符串的组合长度不得超过 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 Table] [run time] Name 和 Namespace 条目引用的字符串的组合长度不得超过 1,023 字节。

Page 186:

第 186 页:

[Method Table] Name (offset in the #Strings stream). 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.

[方法表] 名称(#Strings 流中的偏移量)。方法的名称(不包括方法所属的类的名称)。此项必须以 UTF-8 编码索引长度不超过 1,023 字节的非零字符串。

回答by Yawar Murtaza

Visual Studio 2015 (update 3) - C# 6

Visual Studio 2015(更新 3)- C# 6

Maximum class name length = 993characters

最大类名长度 = 993 个字符

    public class AaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAAAAAZZZ
{            
}

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. Dont know the reason why.

编辑:对于我的 VS2013 Update 3 同事,类名限制 = 1012。不知道是什么原因。