What Characters Are Allowed in C# Class Name

What characters are allowed in C# class name?

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.

Can we have class name with alphanumeric character?

Generally, classes can be named only with alphanumeric characters, meaning a-z, A-Z and 0-9. You should also make the first letter of a class Capital.

Illegal characters in class names

All alphanumeric unicode characters and underscore are valid, but it cannot begin with a number, so anything not belonging in there is invalid.

  • Similar question here
  • More info on C# language spec here

Is using non standard English characters in c# names a bad practice?

There's no technical issues with using non-English characters. C# allows a very large number of Unicode symbols in variable names. The real question is whether or not it is a good idea.

To answer that, you really have to ask yourself a question of "who is my audience?" If your code will only be looked at by French speakers typing on a French layout keyboard, ç is probably a very valid character. However, if you intend your code to be modified by others whose keyboard layout is not French, you may find that that symbol is very hard for them to type. This will mean that, if they want to use your variable name, they'll have to cut/paste it in place because they can't type it directly. This would be a death sentence for any development.

So figure out who your audience is, and limit yourself to their keyboard layout.

What characters are allowed in F# identifiers, module, type and member names?

Take a look at the F# Language Specification 4.0 - In section 3.4 Identifiers and Keywords.

Note that when an identifier is used for the name of a types, union
type case, module, or namespace, the following characters are not
allowed even inside double-backtick marks:

., +, $, &, [, ], /, \\, *, \", `

In addition to this list, the @ (at-sign) is allowed in any name, but will raise a warning:

warning FS1104: Identifiers containing '@' are reserved for use in F# code generation

As near as I can find:

The list of characters can be found in the F# compiler with the name IllegalCharactersInTypeAndNamespaceNames.

As this is used for generating IL, that leads to ECMA-335 - Common Language Infrastructure (CLI) Partitions I to VI which reads:

II.5.3 Identifiers - Identifiers are used to name entities. Simple
identifiers are equivalent to an ID. However, the ILAsm syntax allows
the use of any identifier that can be formed using the Unicode
character set (see Partition I). To achieve this, an identifier shall
be placed within single quotation marks.

ID is a contiguous string of characters which starts with either an

alphabetic character (A–Z, a–z)

or one of _, $, @, ` (grave accent), or ?,

and is followed by any number of

alphanumeric characters (A–Z, a–z, 0–9)

or the characters _, $, @, ` (grave accent), and ?

Is there any restriction for writing property names C#

Why it's not allowing me to add properties having alphanumeric keys

A property name has to be an identifier. Identifiers in C# can't start with a digit. You can have a digit after the first character, but not as the first character.

See section 2.4.2 of the C# 5 specification for the precise details of what is allowed for an identifier. The full grammar is too long to post here usefully, but the crucial part is:

identifier-or-keyword:

   identifier-start-character identifier-part-charactersopt

identifier-start-character:

  letter-character

  _ (the underscore character U+005F)

identifier-part-characters:

  identifier-part-character

  identifier-part-characters identifier-part-character

identifier-part-character:

  letter-character

  decimal-digit-character

  connecting-character

  combining-character

  formatting-character

letter-character:

  A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl

  A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl

Note that the example of 30d is a great one for why it's not allowed - that's actually a numeric literal, of the value 30 and type double. (You've actually got 30dhi of course, but the parser has parsed 30d as a token.)

Which characters are valid in CSS class names/selectors?

You can check directly at the CSS grammar.

Basically1, a name must begin with an underscore (_), a hyphen (-), or a letter(az), followed by any number of hyphens, underscores, letters, or numbers. There is a catch: if the first character is a hyphen, the second character must2 be a letter or underscore, and the name must be at least 2 characters long.

-?[_a-zA-Z]+[_a-zA-Z0-9-]*

In short, the previous rule translates to the following, extracted from the W3C spec.:

In CSS, identifiers (including element names, classes, and IDs in
selectors) can contain only the characters [a-z0-9] and ISO 10646
characters U+00A0 and higher, plus the hyphen (-) and the underscore
(_); they cannot start with a digit, or a hyphen followed by a digit.
Identifiers can also contain escaped characters and any ISO 10646
character as a numeric code (see next item). For instance, the
identifier "B&W?" may be written as "B&W?" or "B\26 W\3F".

Identifiers beginning with a hyphen or underscore are typically reserved for browser-specific extensions, as in -moz-opacity.

1 It's all made a bit more complicated by the inclusion of escaped unicode characters (that no one really uses).

2 Note that, according to the grammar I linked, a rule starting with TWO hyphens, e.g. --indent1, is invalid. However, I'm pretty sure I've seen this in practice.

Special characters in property name

You can make class objects from json using following references

http://json2csharp.com/

Just paste your json string and it will generate C# class and properties inside that json.

How can I generate a safe class name from a file name?

According to the C# spec, the following rules must be adhered to when creating identifiers:

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

This helper will satisfy those conditions:

private static string GenerateClassName(string value)
{
string className = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
bool isValid = Microsoft.CSharp.CSharpCodeProvider.CreateProvider("C#").IsValidIdentifier(className);

if (!isValid)
{
// File name contains invalid chars, remove them
Regex regex = new Regex(@"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Nl}\p{Mn}\p{Mc}\p{Cf}\p{Pc}\p{Lm}]");
className = regex.Replace(className, "");

// Class name doesn't begin with a letter, insert an underscore
if (!char.IsLetter(className, 0))
{
className = className.Insert(0, "_");
}
}

return className.Replace(" ", string.Empty);
}

It first converts the file name to camel case (personal preference), it then uses IsValidIdentifier to determine if the file name is already valid for a class name.

If not, it will remove all invalid characters based on the unicode character classes. It then checks whether the file name starts with a letter, if it does, it prepends an _ to fix it.

Finally, I remove all whitespace (even though it would still be a valid identifier with it).



Related Topics



Leave a reply



Submit