In the following contrived C# code, I don't like that an enum name is the same as the property name.
public enum CollarType
{
Classic,
VNeck
}
public class Shirt
{
public CollarType CollarType { get ... }
}
In the old days when more people were using hungarian / other bizarre naming like class CShirt
, this kind of conflict didn't happen. But today I run into it constantly.
How do you handle this situation? Do you just live with the fact that so many things have the same name, or do you have a better naming scheme?
The only limitation of having the enum name be the same as the property name is that you cannot define the enum as nested type of the class with the property.
E.g. this does not work:
public class Shirt
{
public enum CollarType
{
Classic,
VNeck
}
public CollarType CollarType { get ... }
}
However, on the positive side, it does make the code more readable IMHO because it clearly< shows the association between the enum value and the property:
myShirt.CollarType = CollarType.Classic;
Yes, it's absolutely normal to have a property with the same name as it's type name.
The convention in the BCL is just to run with it. With the exception of nested enums, it doesn't cause any ambiguities.
精彩评论