c# - list property inside of a struct -



c# - list property inside of a struct -

how write property list within of struct?

my code:

public struct config { list<int> ipaddress = new list<int>(); }

if want create auto-property (which default null reference types , cannot initialized) can @scartag suggests.

public struct config { // default null list<int> ipaddress {get; set;} }

however, if you're trying what's in code , initialize actual reference, run issues because can't initialize fields in struct. can have defaults. create matters worse, can't override default constructor you.

generally speaking, struct tends best small, preferably immutable types. there reason don't want utilize class this?

now, if did want create struct "initialized" field, can fool lazy logic:

public struct config { private list<int> _ipaddress; private bool _isassigned; public list<int> { { if (!_isassigned) _ipaddress = new list<int>; homecoming _ipaddress; } set { _ipaddress = value; _isassigned = true; } } }

but really, @ rate it's improve utilize class since can initialize fields, or override default constructor:

public class config { public list<int> ipaddress {get; set;} public config() { ipaddress = new list<int>(); } }

and again, andrew mentioned in comments, suggest looking @ msdn guidance on choosing between struct , class

c#

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -