Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good naming convention to differentiate a class name from a property in C#?

I run into this frequently enough that I thought I'd see what others had to say about it.

Using the StyleCop conventions, I find that I often have a property name that is hard to make different than the class name it is accessing. For example:

public class ProjectManager
{
 // Stuff here
}

public class OtherClass
{
     private ProjectManager ProjectManager { get; set; }
}

It compiles and runs, but seems like it would be an easy way to confuse things, even with the use of "this".

like image 937
Andy Stampor Avatar asked May 18 '10 22:05

Andy Stampor


People also ask

What is the naming convention for classes?

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Which of the following is correct naming convention in C?

Naming Conventions rules for Variables and Methods (Functions) are: It should begin with an alphabet. There may be more than one alphabet, but without any spaces between them. Digits may be used but only after alphabet.

What two conventions you should follow while naming a class?

Class name should begin with capital letter. Example, Bank, School, Student If a class name consists of more than one word, the first letter of each new word should be in uppercase. For example MyClass, AccountDetails, SimpleInterest, etc.


1 Answers

This is actually a very common pattern in .Net programming. Particularly so with enum types and members as it's the .Net Design Guidelines recommended way of programming.

4.0 design guidelines reference

  • http://msdn.microsoft.com/en-us/library/ms229012(v=VS.100).aspx

While it may be a bit confusing, it's not once you've seen it a few times. The tools well support this pattern and given one is a type and the other an instance it's hard to accidentally invert them without causing a compilation error.

like image 152
JaredPar Avatar answered Oct 16 '22 06:10

JaredPar