What characters are allowed in a C # class name?

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

EDIT: indicate. What special characters are allowed? Please be specific, because links to 50 pages of specifications in a high-tech language is not an answer that will help me a lot.

EXPLANATION: What I'm trying to accomplish is to split the class name into distinct parts, for example:

class Person @WorkOffice @Helper @Class

{

}

And I'm thinking of a way to use some character or something else to be able to get parts of this object Person, WorkOffice, Helper and Class.

And yes, I know this is crazy, but I need it. I know that I can use attributes and reflection to store this data in the class meta, but this is not so, so please do not offer this solution.

+55
c # class naming-conventions
Jun 04 '09 at 13:32
source share
5 answers

Details of the specification are here . Essentially, any Unicode character (including Unicode escaping) 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 underscore. In addition, if the identifier is a keyword, you must put an @ sign in front of it. @ Optional otherwise.

+60
Jun 04 '09 at 13:37
source share

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

  • The identifier must begin with a letter or underscore
  • After the first character, it may contain numbers, letters, connectors, etc.
  • If the identifier is a keyword, it must begin with "@"

source

+20
Jun 04 '09 at 13:38
source share

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

And from there you can select most of the things from the groups (from the specifications that others have indicated correctly):

Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf

Remember that Visual Studio (or it's Resharper) will not necessarily love them all, but most of them compile. Take, for example, the character 30FB KATAKANA MIDDLE DOT. It compiles fine, but it does not play well with the IDE. But this weird thing FE34 PRESENTATION FORM FOR VERTICAL WAVY LOW LINE works just fine.

Here's a great separator:

class Personγ€±WorkOfficeγ€±Helperγ€±Class { } 

I am not saying that I recommend using strange characters. But for special occasions, as it seems :)

EDIT: Please note that the specification states that it allows characters from Unicode 3.0 . I missed this and wondered why many characters would not work, although they were from the right groups. More on this subject .

+13
Jul 29 '10 at 9:32
source share

Note that, as thecoop points out, the term β€œcharacter” in the Unicode context is much broader than just letters.

Basically, many Unicode characters can be correctly used in identifiers, even if they can be a bit complex type in Windows.

As an example:

  • Hold ALT
  • Enter "0394" on the keyboard.
  • ALT Release

Greek Delta uppercase will be added to your code ... this is a valid letter with identifier before C #.

Please note, however, that CLS matching is coming out of the window ... but by the sounds you might not have to worry too much about it.

+3
Jun 05 '09 at 2:47
source share

Here's an article you might find useful: C # coding standards and naming conventions

table

In short, usually the first word / part / letter of an object is lowercase and the class is uppercase .

For example:

 HtmlHelper htmlHelper; FtpTransfer ftpTransfer; UIControl uiControl; 
0
Jan 10 '19 at 9:03
source share



All Articles