0

Im starting a new project and i have some problem trying to implement some naming conventions.

I used to work with Classes starting with Uppercase and Singular, like Car or User, and my variables starting with lower case, so if I needed to declare a class that had some variables of type Car and User i would do it like this:

public Car car;
private User user;

Now im trying to use some properties and as i see they should also be PascalCase , wich mean if i need to declare the same examples i would be:

public Car Car { get; set; }
private User User { get; set; }

And you can all see what would the problem be here, or you don't see it as a problem?

So what should i do? what am i missing here?

Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • 5
    Assume I can't see what the problem is. What's is it? – Pete Feb 22 '13 at 14:45
  • It is public and private with small p – manojlds Feb 22 '13 at 14:46
  • Pete i thought of it as a problem, because if i have some static methods in my class and need to access them in the same class that i already have a instance i don't know how to access one or another, but maybe what i was doing wrong was not the naming convention but what i do after. – Luis Tellez Feb 22 '13 at 14:50
  • 2
    @LuisTellez that wouldn't be a problem because you can't call a static method on an instance or vice-versa. So there could be no ambiguity with `Car.{method name}` Either it's a static method and it would be called as such or it's an instance method and would be called using the `Car` property. – D Stanley Feb 22 '13 at 14:52
  • http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29.aspx – Matthew Feb 22 '13 at 14:54
  • I have seen the Naming Guidelines, but i was wrong as seeing this as a problem and didn't realize it could be ok – Luis Tellez Feb 22 '13 at 14:56
  • possible duplicate of [C# naming conventions](http://stackoverflow.com/questions/2109171/c-sharp-naming-conventions) – Steve Wellens Feb 25 '13 at 17:01

6 Answers6

11

The C# naming convention recommends everything that is public as well as classes, interfaces etc., to start with an uppercase letter. The rest should start lower case.

There is no problem with:

private User User { get; set; }

... since the position of each name (word) defines what is what. The English language works the same way. e.g.: "I love love." (pronoun, verb, noun)

Holly
  • 1,305
  • 2
  • 15
  • 30
  • 1
    +1 for there being no problem with using `private User User { get; set; }` however, it's also worth noting that the general recommended naming convention in C# for public members, types and namespaces containing **multiple words** is PascalCase. http://msdn.microsoft.com/en-gb/library/vstudio/ms229043(v=vs.100).aspx – Richard Feb 22 '13 at 15:21
4

What you're run into is called the Color Color problem, because the most common way it crops up is "I need a property called Color of a type called Color". C# has been specifically designed to manage Color Color situations elegantly.

For details, read section 7.6.4.1 "Identical simple names and type names" in the C# 4 specification.

The rules for Color Color situations are a bit complicated (believe me, they do not make the compiler implementer's life any easier!) and they can lead to some interesting corner cases. If this subject interests you then you should read my article on it:

https://ericlippert.com/2009/07/06/color-color/

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

I think in many cases the context means you'd have a specific name - e.g. Car customersCar, etc. Saying that, many people don't have an issue with the name/type being the same - see this link: Should a property have the same name as its type?

For naming conventions in general, following MS isn't a bad start - http://msdn.microsoft.com/en-gb/library/vstudio/ms229045(v=vs.100).aspx

Community
  • 1
  • 1
NDJ
  • 5,189
  • 1
  • 18
  • 27
1

There is no problem there.

Because in the context where you would use the class it can not be misstaken for the property and vice versa.

Edit: Ok, Im going to assume you have the Userclass inside the carclass like this:

public class Car
{
    private class User
    {

    }

    private User User
    {
        get;
        set;
    }
}

Which indeed would create problems. Move out your user and the problem is solved.

public class Car
{

    private User User
    {
        get;
        set;
    }
}

public class User
{

}
Evelie
  • 2,919
  • 2
  • 14
  • 21
1

There is no issue here; As @NDJ suggested you can apply context to add additional prefix to the property if you do not feel comfortable; but this will not generally add additional meaning to the context.

As a general Microsoft style guide encourages the use of Pascal Case for properties.

For a more complete guide on capitalization see the following MSDN article

Kami
  • 19,134
  • 4
  • 51
  • 63
1

Barring the internal class problem that @Evelie pointed out you should not have any issue naming a property the same as the type - in fact this is not an uncommon practice. .NET has public Color Color properties all over the place.

As the following program illustrates the compiler can distinguich between instance calls and static calls:

void Main()
{
    Car c = new Car();
    c.Test();
}

public class Car
{

    public Car()
    {
        User = new User();
    }

    public void Test()
    {
        User.Static();  // calls static method
        User.Instance();   // implies this.User
    }

    public User User { get; set; }
}
// Define other methods and classes here
public class User
{
   public static void Static()
    {
        Console.WriteLine("Static");
    }

    public void Instance()
    {
        Console.WriteLine("Instance");
    }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240