C# 类名中允许使用哪些字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/950616/
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
What characters are allowed in C# class name?
提问by Tomasz Smykowski
What characters are allowed and what is not allowed in a C# class name? Could you please help?
C# 类名中允许使用哪些字符,不允许使用哪些字符?能否请你帮忙?
EDIT: To specify. What special characters are allowed? Please be specific, because links to 50 pages specs in high-technical language is not an answer that will help me a lot.
编辑:指定。允许使用哪些特殊字符?请具体说明,因为指向 50 页高科技语言规范的链接并不是对我有很大帮助的答案。
EXPLANATION: What I try to accomplish is to divide class name into distinguishable parts for example:
解释:我试图完成的是将类名分成可区分的部分,例如:
class Person@WorkOffice@Helper@Class
{
}
class Person@WorkOffice@Helper@Class
{
}
And I think about a way of using some kind of character or something else to be able to get parts Person, WorkOffice, Helper and Class from this class name.
我想到了一种使用某种字符或其他东西的方法,以便能够从这个类名中获取部分 Person、WorkOffice、Helper 和 Class。
And yes, I know it's crazy, but I need it that way. I know that I can use attributes and reflection to store this data in class meta but this is not the case, so please don't suggest this solution.
是的,我知道这很疯狂,但我需要那样。我知道我可以使用属性和反射将这些数据存储在类元中,但事实并非如此,因此请不要建议此解决方案。
采纳答案by thecoop
The spec details are here. Essentially, any unicode character (including unicode escapes) in the character classesLu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, and Cf. The first character is an exception and it must be a letter (classes Lu, Ll, Lt, Lm, or Lo) or an underscore. Also, if the identifier is a keyword, you must stick an @ in front of it. The @ is optional otherwise.
规格详情在这里。本质上,字符类Lu、Ll、Lt、Lm、Lo、Nl、Mn、Mc、Nd、Pc和Cf 中的任何 unicode 字符(包括 unicode 转义符)。第一个字符是一个例外,它必须是字母(类Lu、Ll、Lt、Lm或Lo)或下划线。此外,如果标识符是关键字,则必须在其前面贴上 @。否则@ 是可选的。
回答by Jeremy Coenen
Valid identifiers in C# are defined in the C# Language Specification, item 9.4.2. The rules are very simple:
C# 中的有效标识符在 C#语言规范9.4.2 项中定义。规则非常简单:
- An identifier must start with a letter or an underscore
- After the first character, it may contain numbers, letters, connectors, etc
- If the identifier is a keyword, it must be prepended with “@”
- 标识符必须以字母或下划线开头
- 在第一个字符之后,它可能包含数字、字母、连接符等
- 如果标识符是关键字,则必须以“@”开头
回答by jerryjvl
Note that as thecoop indicates, the term 'character' in the context of Unicode is a lot broader than just alphabetical letters.
请注意,正如 thecoop 指出的那样,Unicode 上下文中的术语“字符”比字母字母要广泛得多。
Basically a lot of Unicode symbolscan be validly used in identifiers, even if they can be a bit tough to typein Windows.
基本上,很多Unicode 符号都可以有效地用于标识符中,即使它们在 Windows 中可能有点难以输入。
As an example:
举个例子:
- Hold down ALT key
- Type '0394' on the keypad
- Release ALT
- 按住 ALT 键
- 在键盘上输入“0394”
- 释放ALT
Will add a greek uppercase Delta to your code... this is a valid identifier letter as far as C# is concerned.
将在您的代码中添加一个希腊大写的 Delta... 就 C# 而言,这是一个有效的标识符字母。
Note however that CLS compliance goes out the window... but by the sounds of it you may not be too concerned about that anyway.
但是请注意,CLS 合规性已经过时了......但从它的声音来看,无论如何您可能不会太在意。
回答by asgerhallas
The Unicode categories can be found here: http://www.dpawson.co.uk/xsl/rev2/UnicodeCategories.html
Unicode 类别可以在这里找到:http: //www.dpawson.co.uk/xsl/rev2/UnicodeCategories.html
And from there you can pick most things from within the groups (from the specs, that others have correctly pointed to too):
从那里你可以从组中选择大部分内容(从specs,其他人也正确指出):
Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf
Lu、Ll、Lt、Lm、Lo、Nl、Mn、Mc、Nd、Pc、Cf
Be aware though, that Visual Studio (or is it Resharper) won't necessarily be fond of them all, but most of them do compile. Take for example the character 30FB KATAKANA MIDDLE DOT. It compiles fine, but it doesn't play nice with the IDE. But this strange thingy FE34 PRESENTATION FORM FOR VERTICAL WAVY LOW LINE works just fine.
但请注意,Visual Studio(或者是 Resharper)不一定都喜欢它们,但它们中的大多数都可以编译。以字符 30FB KATAKANA MIDDLE DOT 为例。它编译得很好,但它在 IDE 中表现不佳。但是这个奇怪的东西 FE34 PRESENTATION FORM FOR VERTICAL WAVY LOW LINE 工作得很好。
Here's a seperator that works fine:
这是一个工作正常的分隔符:
class Person?WorkOffice?Helper?Class
{
}
I'm not saying I recommend using strange characters though. But for special occasions as this seems to be :)
不过,我并不是说我建议使用奇怪的字符。但对于特殊场合,这似乎是:)
EDIT: Take note that the specification says to that it allows characters from Unicode 3.0. I overlooked that and wondered why a lot of characters wouldn't work, though they were from the right groups. Check this question for details.
编辑:请注意,规范说它允许来自 Unicode 3.0 的字符。我忽略了这一点,想知道为什么很多角色都不起作用,尽管他们来自正确的群体。检查此问题以获取详细信息。
回答by V.7
Here's a an article you might find helpful: C# Coding Standards and Naming Conventions
这是一篇您可能会觉得有帮助的文章:C# 编码标准和命名约定
In short, in common, first word/part/letter of object is lowercasewhile class's is uppercase.
简而言之,一般来说,对象的第一个单词/部分/字母是小写的,而类是大写的。
For example:
例如:
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
回答by thargy
Based on the character classed in the above answers, you can check a character using the below extension method:
根据上述答案中分类的字符,您可以使用以下扩展方法检查字符:
public static bool IsValidInIdentifier(this char c, bool firstChar = true)
{
switch (char.GetUnicodeCategory(c))
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.TitlecaseLetter:
case UnicodeCategory.ModifierLetter:
case UnicodeCategory.OtherLetter:
// Always allowed in C# identifiers
return true;
case UnicodeCategory.LetterNumber:
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.DecimalDigitNumber:
case UnicodeCategory.ConnectorPunctuation:
case UnicodeCategory.Format:
// Only allowed after first char
return !firstChar;
default:
return false;
}
}