views:

234

answers:

5

Since the standard c# convention is to capitalize the first letter of public properties, the old c++ convention of initial capital for type names, and initial lowercase for non-type names does not prevent the classic name collision where the most obvious object name matches the type name:

class FooManager
{
    public BarManager BarManager { get; set; } // Feels very wrong.
                                               // Recommended naming convention?
    public int DoIt()
    {
         // 1st and 2nd Bar Manager are different symbols 
         return BarManager.Blarb + BarManager.StaticBlarb;                                                                          
    }
}

class BarManager
{
    public        int Blarb { get; set; }
    public static int StaticBlarb { get; set; }
}

It seems to compile, but feels so wrong. Is there a recommend naming convention to avoid this?

+1  A: 

The c# convention is to name properties in the same way as you name your classes. The reason you feel it's wrong is because you come from a different background. But if you use if for a while you will learn that it doesn't cause you any problems and it will feel pretty natural when you are used to it. There is no place when it will collide in any way. I (as a c# developer) feel that the convention of initial lower case letter for properties feel wrong. You just have to get used to it.

Mattias Jakobsson
+7  A: 

Having a type and a property with the exact same name isn't uncommon. Yes, it looks a little weird, but renaming properties to avoid this clash looks even weirder, admittedly.

Eric Lippert had a blog post on this exact topic.

There is no ambiguity for the compiler, however.

Joey
That blog posts really points out why in other languages it's often illegal and that ambiguity exists in the language itself. Resolving the name collision does not resolve the ambiguity because c# will call static methods through the instance which seems to be a reason that the initial capital convention is reasonable in c#/.netI like your answer but I think the ambiguity addressed in the blog post should be addressed too.
Catskul
@Catskul, The only thing that would collide is static properties really. And there is no reason to use static properties. Methods will not collide (as you will have to use "()" when you call a method).
Mattias Jakobsson
@Mattias check out that blog post. It talks about how a static "Print( string )" might cause a problem if there is also a "Print( object )".
Catskul
+1  A: 

I'm okay with this honestly -- if your static methods/members aren't obviously static by name and by purpose, you've got bigger problems than name collision.

Cory Petosky
*"if your static methods/members aren't obviously static by name and by purpose, you've got bigger problems than name collision."* I agree with this for the most part, but I think there is room for this not to be the case 100% of the time.
Catskul
+1  A: 

I do not think it has ever caused an issue for me. Following are general conventions, for Properties. Only thing helpful could be getting used to these....

  • Pascal Case, no underscores.
  • Try to avoid abbreviations.
  • Members must differ by more than case to be usable from case-insensitive languages like Visual Basic .NET.

Why: This convention is consistent with the .NET Framework and is easy to read. like

public int RecordId

reference : NET Programming Standards and Naming Conventions

also check this: General Naming Conventions

Asad Butt
A: 

I will caviat this by saying, I don't mind the collision, as the compiler can work it out. However, in the spirit of offering other solutions I have seen, and letting others decide for myself... I think this is where the ugly (to me) pattern of using My* arose.

For instance:

public class Foo { /* ... */ }

public class Bar
{
    public Foo MyFoo { get; set; }
    // ...
}
Nick