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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -