C# naming related enum, private member and public accessor.
Might sound petty of me, but I struggle with C# naming convention. It is fine when I only have a private member and a public accessor as follow:
private int size;
public int Size { get { return size;} }
but it gets confusing when you use an enum. Clearly, I can't name my accessor the same as my enum :
public enum Size { Tiny = 0, Small = 1, Medium = 2, Large = 4}
private Size size;
public Size Size { get { return size;} }
By convention I shouldn't use plural enum names unless they are bitwise which it isn't. The following could solve it, but I doubt that's coding friendly when it is only a simple getter.
public Size GetSize(){ return size; }
Is there other alternatives?
Answer by Jeshira · Jan 25, 2018 at 06:14 PM
After a bit more research, I managed to find a bit of counter-intuitive information. The suggested structure is to pull the enum out of my class :
public class Item {
Size size;
public Size Size { get { return size; } }
}
public enum Size {
tiny = 0,
small = 1,
medium = 2,
large = 4
}
This way you are not conflicting with naming convention. Problem I see with this is you must not have more than one Size enum in the namespace.
public class Weapon : Item {
Type type;
public Type Type { get { return type; } }
}
public class Armor : Item {
Type type;
public Type Type { get { return type; } }
}
We'll agree armor and weapon can't have the same type. Can't nest enum due to original issue to not conflict with naming convention. So only solution left would be to rename Type into ArmorType and WeaponType, and if we are renaming them it won't conflict with accessor name right? :
public class Weapon : Item {
public enum WeaponType {
ranged,
melee
}
WeaponType type;
public WeaponType Type { get { return type; } }
}
But how fun will it be to type :
Weapon.WeaponType type = Weapon.WeaponType.ranged;
So no nesting :
public class Weapon : Item {
WeaponType type;
public WeaponType Type { get { return type; } }
}
public enum WeaponType {
ranged,
melee
}
Or you can look at it the way that you changed the name from Size
to ItemSize
.
In general, I'm avoiding giving too generic names, even if being within a class makes it clearer, but in the end it is nicer to have the enum independently of the class it will mostly be used, or even in a separate .cs file.
I agree. Why I say it is counter-intuitive is that it would make sense to have Item.Size.tiny
, and as you say best is to avoid generic names, but Size or Type are and more are difficult to avoid without making it confusing with synonyms.
Absolutely, that's what I always tried to go for! But by now I've ran so many times into the problem of the getter being named the same as the enum type, that I don't do it anymore. Well, that's C#...
I also tend to keep my namespaces (especially the global one) as clean as possible. However you should only nest types if they really only have a meaning to the outer class. If you may use the nested type elsewhere / outside the outer class it's better to declare it seperately. By convention it's actually recommended to use a seperate file which makes it easier to find it in the folder structure.
Answer by unity_3-HQNOBbN6qQng · Jan 25, 2018 at 06:08 PM
put a underscore _ before your private filed. it's very common in C#. like this:
public enum Size { Tiny = 0, Small = 1, Medium = 2, Large = 4}
private Size _size;
public Size Size { get { return _size;} }
Adding an underscore changes nothing to the problem unless you plan to rename accessor with lowercase, but doing so you conflict with na$$anonymous$$g convention of accessors.
Right, the problem is not the private field but the fact that the class has two "things" named "Size", one nested type and one property.
Your answer
![](https://koobas.hobune.stream/wayback/20220612151254im_/https://answers.unity.com/themes/thub/images/avi.jpg)