views:

1391

answers:

5

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 a answer that will help me a lot.

EXPLANATION: What I try to acomplish 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 not suggest this solution.

+8  A: 

The spec details are here. Essentially, any unicode character (including unicode escapes) in the character classes Lu, Ll, Lt, Lm, Lo, or Nl, apart from the first letter which has to be a letter or underscore (_). Also, if the identifier is a keyword, stick an @ in front of it.

thecoop
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.
tomaszs
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
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??
tomaszs
Correct. Plus that two consecutive underscore characters ("__") should not be used. They are reserved for internal use.
0xA3
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
Do you want to say that I can not use nothing more than letters and numbers and single underscores?
tomaszs
+6  A: 

Valid identifiers in C# are defined in the C# Language Specification, item 2.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

Jeremy Coenen
Could you be more specific? I've detailed in question what I look for. Hope to see more help. Thanks!
tomaszs
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
A: 

thecoop is correct - also it is important to be aware of CLS compliance when designing new types.

Andrew Hare
Still don't have a answer. I specified what I look for in question. Please read it.
tomaszs
+2  A: 

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 '+' on the keypad
  • 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
+1  A: 

The Unicode categories can be found here: 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

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

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.

asgerhallas