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.
我想到了一种使用某种角色或其他东西来从这个类名中获取Part,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.
是的,我知道这很疯狂,但我需要这样。我知道我可以使用属性和反射将这些数据存储在类meta中,但事实并非如此,所以请不要建议这个解决方案。
4 个解决方案
#1
56
The spec details are here. Essentially, any unicode character (including unicode escapes) in the character classes Lu, 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,N1,Mn,Mc,Nd,Pc和Cf中的任何unicode字符(包括unicode转义)。第一个字符是一个例外,它必须是一个字母(Lu,Ll,Lt,Lm或Lo类)或下划线。此外,如果标识符是关键字,则必须在其前面加上@。否则,@是可选的。
#2
19
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 “@”
- 如果标识符是关键字,则必须以“@”为前缀
资源
#3
13
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):
从那里你可以从组中挑选大多数东西(从规格,其他人也正确指出):
Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf
Lu,L1,Lt,Lm,Lo,N1,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演示形式垂直波浪低线工作得很好。
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中的字符。我忽略了这一点,并想知道为什么很多角色都不会工作,尽管他们来自正确的群体。查看此问题了解详情。
#4
3
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 symbols can be validly used in identifiers, even if they can be a bit tough to type in Windows.
基本上,许多Unicode符号可以在标识符中有效使用,即使它们在Windows中输入有点困难。
As an example:
举个例子:
- Hold down ALT key
- 按住ALT键
- Type '0394' on the keypad
- 在键盘上输入“0394”
- Release ALT
- 释放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合规性已经消失了......但是通过它的声音,你可能不会太在意它。
#1
56
The spec details are here. Essentially, any unicode character (including unicode escapes) in the character classes Lu, 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,N1,Mn,Mc,Nd,Pc和Cf中的任何unicode字符(包括unicode转义)。第一个字符是一个例外,它必须是一个字母(Lu,Ll,Lt,Lm或Lo类)或下划线。此外,如果标识符是关键字,则必须在其前面加上@。否则,@是可选的。
#2
19
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 “@”
- 如果标识符是关键字,则必须以“@”为前缀
资源
#3
13
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):
从那里你可以从组中挑选大多数东西(从规格,其他人也正确指出):
Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf
Lu,L1,Lt,Lm,Lo,N1,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演示形式垂直波浪低线工作得很好。
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中的字符。我忽略了这一点,并想知道为什么很多角色都不会工作,尽管他们来自正确的群体。查看此问题了解详情。
#4
3
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 symbols can be validly used in identifiers, even if they can be a bit tough to type in Windows.
基本上,许多Unicode符号可以在标识符中有效使用,即使它们在Windows中输入有点困难。
As an example:
举个例子:
- Hold down ALT key
- 按住ALT键
- Type '0394' on the keypad
- 在键盘上输入“0394”
- Release ALT
- 释放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合规性已经消失了......但是通过它的声音,你可能不会太在意它。