C # 类名中允许哪些字符?

C # 类名中允许什么字符,不允许什么字符? 您能帮帮忙吗?

编辑: 指明。允许什么特殊字符?请具体,因为链接到50页规格的高科技语言不是一个答案,将帮助我很多。

说明: 我试图完成的是将类名划分为可区分的部分,例如:

类 Person@WorkOffice@Helper@Class

{

}

我想到了一种使用某种字符或其他东西从这个类名称中获得 Person、 WorkOffice、 Helper 和 Class 部分的方法。

是的,我知道这很疯狂,但我需要这样。我知道我可以使用属性和反射将这些数据存储在类元中,但事实并非如此,所以请不要提出这个解决方案。

74369 次浏览

The spec details are here. Essentially, any unicode character (including unicode escapes) in the character classes Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, character classes0, character classes1, and character classes2. 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.

Valid identifiers in C# are defined in the C# Language Specification, item 9.4.2. The rules are very simple:

  • 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 “@”

source

Note that as thecoop indicates, the term 'character' in the context of Unicode is a lot broader than just alphabetical letters.

Basically a lot of Unicode symbols can be validly used in identifiers, even if they can be a bit tough to type in Windows.

As an example:

  • Hold down ALT key
  • Type '0394' on the keypad
  • Release ALT

Will add a greek uppercase Delta to your code... this is a valid identifier letter as far as C# is concerned.

Note however that CLS compliance goes out the window... but by the sounds of it you may not be too concerned about that anyway.

The Unicode categories can be found here: http://www.dpawson.co.uk/xsl/rev2/UnicodeCategories.html

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

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.

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 :)

Take note that the specification says 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.

Here's a an article you might find helpful: C# Coding Standards and Naming Conventions

table

In short, in common, first word/part/letter of object is lowercase while class's is uppercase.

For example:

HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;

Based on the character classed in the existing answers, you can check a character using this 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;
}
}