c# - When exactly do I use a struct (Dont tell me when I want things to be allocated on a stack) -
a lot of times answer merely when want things allocated on stack instead of heap.. assuming dont know stack , heap (and please dont try explain here), when should using structs instead of classes?
here answer i've been giving out, please tell me if i'm wrong or falling short of better answer:
i create structs when have enums want add more data to. instance, might start simple enum:
public enum colors { blue, green, red } then if need store more aspects of data go structs:
public struct color { string name; int hexvalue; string description; } public class colors { public static color blue; public static color red; public static color green; static colors() { blue = new color("blue", 1234, "a light blue" } } the point is... similar enums, use structs when want declare bunch of types.
struct vs class in .net
the real time use struct when want value type properties. value types behave differently reference types, , difference can shocking , bug inducing if aren't aware. example struct copied (and out of) method calls.
the heap vs stack isn't compelling argument me. in typical .net app, how care object lives?
i use structs in .net apps. place i've used them in game wanted value type properties objects vectors , such.
struct vs class in c++
this simpler question answer. structs , classes in c++ identical each other 1 minor difference. in c++ struct public default, in c++ class private default. difference.
Comments
Post a Comment