c# - Local variable and Global variable with same name and how it works? -
can please tell me program flow , out of given snippet.i tried on vs , got 0 0 output,and want know how works. thanks.
static void main(string[] args) { sample s1 = new sample(); s1.getdata(10, 5.4f); s1.displaydata(); } class sample { int i; single j; public void getdata(int i,single j) { = i; j = j; } public void displaydata() { console.writeline(i + " " + j); } }
since local variables preferred on class variables, class variables never set. inside getdata
method setting local (method scoped) variables own value. hence, in displaydata
method, printing default values of integers (0).
to fix this, can either change names of variables, prefixing them example, or use this
set scope.
public void getdata(int i,single j) { this.i = i; this.j = j; }
Comments
Post a Comment