77

What characters are allowed and what is not allowed in a C# class name? Could you please help?

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.

EXPLANATION: What I try to accomplish is to divide class name into distinguishable parts for example:

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.

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.

mireazma
  • 526
  • 1
  • 8
  • 20
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236

6 Answers6

80

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.

flq
  • 22,247
  • 8
  • 55
  • 77
thecoop
  • 45,220
  • 19
  • 132
  • 189
  • 1
    I specified more what I search for. Link you provided is very complicated and I think that do not have time to search over thousands of pages to get what characters are allowed. – Tom Smykowski Jun 04 '09 at 13:45
  • As I said, it boils down to - any letter or _ for the first character - any unicode character for the rest (the character classes Lu, Ll, Lt, Lm, Lo, or Nl) - if it is a keyword, add a @ to the start – thecoop Jun 04 '09 at 13:47
  • 1
    I can use underscore _ inside my class name, but can't @ nor !. Are there any special characters that I can use? What is Lu, LI, Lt, Lm, Lo, NI?? – Tom Smykowski Jun 04 '09 at 13:48
  • Correct. Plus that two consecutive underscore characters ("__") should not be used. They are reserved for internal use. – Dirk Vollmar Jun 04 '09 at 13:49
  • 5
    Character class information is at http://msdn.microsoft.com/en-us/library/20bw873z.aspx. Unfortunately, unicode is so big it's infeasible to give a definitive list without referring to character classes :( – thecoop Jun 04 '09 at 13:50
  • 2
    Do you want to say that I can not use nothing more than letters and numbers and single underscores? – Tom Smykowski Jun 04 '09 at 13:53
  • 7
    @tomaszs No, he means that there are so many characters you can use that it wouldn't be practical to list them all here. For example you can use accented characters : `àéôùÿ...`, and there's a plethora of them. – Suzanne Soy Sep 26 '13 at 18:30
  • 8
    There appear to be some exceptions to this. For example, I can't declare a class named `Testˆ` - despite this last character (`U+02C6`) being in the Lm category – fostandy Nov 23 '15 at 05:04
  • 2
    I don't think the compiler used by Visual Studio adheres to this claim. I can't, for example, compile with a class named "testᴺᵁᴸᴸ", and get Error CS1056: Unexpected Character (these characters are from Lm by the way.) Would appreciated clarity from poster on this. – TernaryTopiary Jan 05 '17 at 07:33
  • 1
    W.R.T my prior comment; have checked other answers and it's worth mentioning that **support for characters above Unicode 3.0 is not available**, which seems important to mention! – TernaryTopiary Jan 05 '17 at 07:38
  • @TernaryTopiary: Well.. in VS2017 v15.5.4 community, I successfuly created a local variable called `thisˆthisᴺᵁᴸᴸthis` – quetzalcoatl Feb 07 '18 at 19:52
  • 1
    The character classes mentioned in this answer are: Lu = "Letter, Uppercase". Ll = "Letter, Lowercase". Lt = "Letter, Titlecase". Lm = "Letter, Modifier". Lo = "Letter, Other". Nl = "Number, Letter". Mn = "Mark, Nonspacing". Mc = "Mark, Spacing Combining". Nd = "Number, Decimal Digit". Pc = "Punctuation, Connector". Cf = "Other, Format". – WalterGR Dec 03 '19 at 23:45
  • Almost took it that I'm looking at [chemical elements](https://en.wikipedia.org/wiki/List_of_chemical_elements). – Pang Feb 05 '21 at 06:19
27

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

flq
  • 22,247
  • 8
  • 55
  • 77
Jeremy Coenen
  • 1,085
  • 1
  • 12
  • 19
  • Could you be more specific? I've detailed in question what I look for. Hope to see more help. Thanks! – Tom Smykowski Jun 04 '09 at 13:46
  • 1
    No special charecters are allowed other than the @ or _ symbol. It has to start with a letter underscore or @ symbol and after that you are good to go. – Jeremy Coenen Jun 04 '09 at 13:51
  • @JeremyCoenen Starting with `@` is just for escaping keywords (`@if`), but then the next character must follow the same rules for the first. E.g. I cannot name a variable `@1abc`, because a number is not allowed as first character. – Teejay Aug 08 '17 at 09:51
  • 2
    In case of dead reference: https://web.archive.org/web/20130315082540/http://blog.visualt4.com/2009/02/creating-valid-c-identifiers.html – Artfaith Jan 10 '19 at 08:52
21

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
asgerhallas
  • 16,890
  • 6
  • 50
  • 68
7

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;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
thargy
  • 161
  • 2
  • 4
3

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.

jerryjvl
  • 19,723
  • 7
  • 40
  • 55
  • 2
    If you switch your keyboard entry from english to greek you can input Greek letters fast for use in mathematical expressions. This: `σ=E*ε;` is a valid statement. – John Alexiou Jun 20 '12 at 18:47
-6

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;
Artfaith
  • 1,183
  • 4
  • 19
  • 29