c# 4.0 - How do I call a getter or setter in C# -


i understand how create getters , setters

public myclass {     public int myval { get; set; }      // more stuff } 

but don't understand how call later on.

public myotherclass {     public myotherclass()     {          myclass localmyclass = new myclass();           localmyclass.???set??? = 42;            // intelisense doesn't seem give obvious options after enter           // period.     } } 

how should set value of myval in localmyclass?

localmyclass.myval = 42; 

getters , setters let treat values public properties. difference is, can whatever want inside functions getting , setting.

examples:

store other variables

private int _myval, myotherval; public int myval { get; set { _myval = value; myotherval++; } } 

make numbers / return constants

public int myval { { return 99; } set; } 

throw away setter

private int _myval; public int myval { { return _myval; } set { ; } } 

in each of these cases, user feel it's public data member, , type

localmyclass.myval = 42; int = localmyclass.myval; 

the gettors , settors let make implementation of own. also, hogan says, "there number of libraries , add-ons [e.g. mvc.net] require use getter , setter functions" - if it's trivial {get; set;} case.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -