11

Is it safe to use _ in class names?

What's the best replacement for . in naming? (Currently I use _.)

2
  • Have you tried it and seen what the compiler says? Commented Jan 6, 2011 at 23:50
  • 1
    @BoltClock: This is a matter of best practice rather than working or not working. Commented Jan 6, 2011 at 23:53

6 Answers 6

16

It's safe to do so for one underscore at a time, although you shouldn't use two consecutive underscores. From the C# language spec, section 2.4.2:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

Personally I try to avoid type names with underscores anyway though... I'm not sure what you mean by "replacement for ." though. If you could give some context, that would help.

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't that only refer to 2 underscores starting the name? Not anywhere within it?
Replace for . I mean if you have a class to do something with a website, since the address contains . what's your choice for replacing . ?
@Xaqron: I would use "Dot" if I absolutely had to - although most websites wouldn't need the full address anyway (e.g. StackOverflow, Slashdot, Xkcd etc)
5

As others have mentioned, it is safe. However, Microsoft's Class Naming Guidelines read:

Do not use the underscore character (_).

As Jon Skeet mentioned, a good alternative would be to replace . with Dot. For example, a class related to drive.google.com could be named DriveDotGoogle. (I think the DotCom portion could be omitted so as not to unnecessarily lengthen the name.)

2 Comments

Interesting then that the tool @csharptest.net mentions, from MS, says "Any invalid tokens are replaced with the underscore (_) character"
Because DriveDotGoogle is way more readable than Drive_Google right? Imho this is a horrible decision for their conventions.
3

Yes, it is safe.

You should never even consider having a "." in a class name. I can't imagine when they would come up. Perhaps if you wanted to name something ".NET" you would do with "DotNet". Class names should be concise, and personally, I've never used an underscore in one. I prefer camel casing.

You may consider reading MSDN - Naming Guidelines.

4 Comments

+1 for Dot idea. So I can use: GoogleDotCom when need a .
@Xaqron: Yeah, but in general you shouldn't need it (you could represent that it is a .com website via another property, etc, it doesn't need to be in the class name).
Note that every type you write that is contained within a namespace technically does have a dot in it. On the CIL level, there is no such thing as a namespace, and so type names will be Namespace.Typename. The namespace construct is effectively a high-level language feature that parses dots as namespace separators.
as MSDN Naming Class Guidelines refered by @NoonSilk there says "....Do not use the underscore character (_)... "
2

For class Names, I use CamelCasing rather than _. You should stick to one standard, this is specially important if you're working on projects with more than one person. Naming convention should be consistent within the solution / organisation

Comments

0

It is safe to use _ in class names, just like it is with other identifiers.

Why do you need a logical . in your class names? Can you give an example?

Comments

0

Sounds like your generating CSharp code. If this is the case you may want to look at the following helper routine from the System.Design assembly:

http://msdn.microsoft.com/en-us/library/ms145952.aspx

static string FixName(string givenName) { return System.Resources.Tools.StronglyTypedResourceBuilder.VerifyResourceName( givenName, new Microsoft.CSharp.CSharpCodeProvider()); } 

1 Comment

If you're generating code you should look at the T4 templating engine, which MS developed for precisely this use-case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.