Monday 15 July 2013

c# - Program output what & why? -


This program has Outlook: 0

Can anyone please tell me that Production is coming?

  Namespace console application {class sample {int i; Single J; Public Zero SetData (Int'l, Single J) {i = i; J = j; } Public Zero Exposure () {Console.WriteLine (i + "" + j); }} Class MyProgram {static zero main (string [] args) {sample s1 = new sample (); S1.SetData (10, 5.4f); S1.Display (); }}    

You get the output of 0 from set data < There are local variables in the / code> method, variable i and j method and for this reason your class level variable i and j < / Code> is not being assigned to.

  Public Zero Set Data (Int i, Single J) {I = i; J = j; }   

Change the above code to:

  Public Zero Setdata (Int i, Single J) {this.i = i; // Using it will refer to class-level variable this.j = j; }   

Or you can name the local value differently, then the local variable can not hide the class-level variable.

  Public Zero SetData (int a, single b) {i = A; J = B; }   

Now output will be 10 and 5.4

No comments:

Post a Comment