Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What characters are allowed in C# class name?

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.

like image 812
Tom Smykowski Avatar asked Jun 04 '09 13:06

Tom Smykowski


People also ask

What characters are allowed in C function?

A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. The first letter of an identifier should be either a letter or an underscore.

What are the 256 characters in C?

We have 256 character to represent in C (0 to 255) like character (a-z, A-Z), digits (0-9) and special character like !, @, # etc. This each ASCII code occupied with 7 bits in the memory. Let suppose ASCII value of character 'C' is 67. When we give input as 'B' machine treat it as 67 internally and stores its address.

How many types of character set is available in C language?

The C character set consists of upper and lowercase alphabets, digits, special characters and white spaces. The alphabets and digits are altogether called as the alphanumeric character. A variable is an entity that has a value and is known to the program by name.

Can we use special characters in variable in C?

The C rule of declaring variable name says that variable name starts with the underscore but not with other special characters, but $ can also be used right.


2 Answers

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.

like image 55
thecoop Avatar answered Sep 17 '22 10:09

thecoop


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

like image 22
Jeremy Coenen Avatar answered Sep 21 '22 10:09

Jeremy Coenen