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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -