c# - Variable value assign and retrieve -
how variable assigning (setting) , retrieve value works in background? example:
{ int myvalue; myvalue = 5; //here assign value (set) console.writeline(myvalue); // here retrieve value }
so here first declare value. in next line assign value (does work same way setting property). also, when retrieving value, work same way in properties.
next question:
if have textbox control, has text property. lets say, name of text box control mytextbox
.
mytextbox.text = "michael"; // here set value string result = mytextbox.text; // here retrieve (get) value
does works same { get; set; }
method in fields-properties?
you can @ il if want. here's simple sample using linqpad:
void main() { int ; i=5 ; i.dump(); i_p = 6; i.dump(); } // define other methods , classes here public int i_p {get; set;}
and here's il it:
il_0000: nop il_0001: ldc.i4.5 il_0002: stloc.0 // il_0003: ldloc.0 // il_0004: call linqpad.extensions.dump il_0009: pop il_000a: ldarg.0 il_000b: ldc.i4.6 il_000c: call userquery.set_i_p il_0011: nop il_0012: ldloc.0 // il_0013: call linqpad.extensions.dump il_0018: pop il_0019: ret get_i_p: il_0000: ldarg.0 il_0001: ldfld userquery.<i_p>k__backingfield il_0006: stloc.0 il_0007: br.s il_0009 il_0009: ldloc.0 il_000a: ret set_i_p: il_0000: ldarg.0 il_0001: ldarg.1 il_0002: stfld userquery.<i_p>k__backingfield il_0007: ret
nothing fancy happening there, if asking.
the point of having properties avoid having public variables, , enable encapsulate (read hide), implementation.
so, using property, can in setter alter it, apply validation logic, change other variables, etc, (you shouldn't, btw), or better yet, in future, if need change backing variable type, can without breaking public interfaces.
Comments
Post a Comment