c# - When to use an Enum vs Struct -
in our database have table called objecttype. table contains series of rows id , and objectname. in our back-end code, handle different objects in different ways, it's required refer id of object comparisons. these ids same, , map same object.
what construct better handling these objects if methods expecting integer value passed them?
when use struct? when use enum?
enumerations aren't interchangable structs. have different usages. struct can have members , methods, enumeration can have labels , values labels.
you use enum specify different behaviours of system
let's have class person , let's whatsoever reason person can have emotional state.
a implementation use enum this:
enum emotionalstate { happy = 3, sad, shy } you can use enum items integers :
int b = (int)emotionalstate.sad // <---4 and in reverse direction
int v = 3; emotionalstate s = (emotionalstate)v; // happy note enum members don't have type. assigned starting value. example if assign 3 on first item next item marked 4.
a example struct point
struct point { int a; int b; } structs should small value types , kept on stack reference types kept on heap.
Comments
Post a Comment