C# Programming - Classes and Objects - Discussion
Discussion Forum : Classes and Objects - General Questions (Q.No. 6)
6.
Which of the following will be the correct output for the C#.NET program given below?
namespace IndiabixConsoleApplication
{
class Sample
{
int i;
Single j;
public void SetData(int i, Single j)
{
i = i;
j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
class MyProgram
{
static void Main(string[ ] args)
{
Sample s1 = new Sample();
s1.SetData(10, 5.4f);
s1.Display();
}
}
}
Discussion:
28 comments Page 1 of 3.
Ravindra Maurya said:
9 years ago
It is why because we are calling same argument name, if we make its Letter Caps value will be print or it 'll be print when argument name will be different compiler is calling default constructor when we used same arguments.
Sample s1 = new Sample();
s1.SetData(10, 5.4f,8);
s1.Display();
private int i;
private Single J;
private int b;
public void SetData(int I, Single j,int B)
{
i = i;
J = j;
b = B;
}
public void Display()
{
Console.WriteLine(i + " " + J +" "+b);
}
Sample s1 = new Sample();
s1.SetData(10, 5.4f,8);
s1.Display();
private int i;
private Single J;
private int b;
public void SetData(int I, Single j,int B)
{
i = i;
J = j;
b = B;
}
public void Display()
{
Console.WriteLine(i + " " + J +" "+b);
}
(1)
Gufran Ali said:
1 decade ago
class Sample
{
int i;
Single j;
public void SetData(int i, Single j)
{
i = i;
j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
In the above code we are using local variables with same name as instance variable, because local variable will hide the instance variable. Instance variables will not get initialize. To refer the instance variable in case of local variable name is same as instance variable, use this keyword.
like this.i = i; //here this.i refer to instance variable i.
{
int i;
Single j;
public void SetData(int i, Single j)
{
i = i;
j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
In the above code we are using local variables with same name as instance variable, because local variable will hide the instance variable. Instance variables will not get initialize. To refer the instance variable in case of local variable name is same as instance variable, use this keyword.
like this.i = i; //here this.i refer to instance variable i.
Nitin kale said:
1 decade ago
Here in the above:
public void SetData(int i, Single j)
{
i = i;
j = j;
}
We are trying to assign the value to the local variable that is,
i=i;
j=j;
So to resolve this problem we should write like :
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
This is an object of current class ,when the function call like:
sample obj1=new sample();
Then 'this' object contains the address of obj1 and now this directly referred to the object to obj1 and now it will store the value into the data field of object.
public void SetData(int i, Single j)
{
i = i;
j = j;
}
We are trying to assign the value to the local variable that is,
i=i;
j=j;
So to resolve this problem we should write like :
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
This is an object of current class ,when the function call like:
sample obj1=new sample();
Then 'this' object contains the address of obj1 and now this directly referred to the object to obj1 and now it will store the value into the data field of object.
Durg said:
1 decade ago
In this, as s1 is created, default constructor initialize field variable i, j by default value.
i.e, i = 0; j = 0;
-------------
When we are calling s1.SetData(10, 5.4f) , we assign value to local variable i, j; which we define in formal argument ( not field variable )
That's why, compiler generate warning ---
" value is assign to same variable".
-------------
If you want to initialize field variable then
1. use 'this' keyword ,like this.i and this.j
2. use different name for parameter variable.
i.e, i = 0; j = 0;
-------------
When we are calling s1.SetData(10, 5.4f) , we assign value to local variable i, j; which we define in formal argument ( not field variable )
That's why, compiler generate warning ---
" value is assign to same variable".
-------------
If you want to initialize field variable then
1. use 'this' keyword ,like this.i and this.j
2. use different name for parameter variable.
Rahul Kumar said:
1 decade ago
The code is write is perfect but the on the time of value initialization it's initializing himself rather than to the variable declares in the class. Have a look in the below snippet.
class Sample
{
int i;
Single j;
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
class Sample
{
int i;
Single j;
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
Ramesh Malode said:
1 decade ago
Instance variable name and local variable name of method is same, that's why local variable hide the instance variable, if you want to run this, you should be use "this" keyword as like :
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
If you run without "this" then result remains as zero only.
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
If you run without "this" then result remains as zero only.
Bhavin said:
1 decade ago
See the argument names and class variable names are same. So it assigns values to argument variables itself and values of class level variables will remain as it is default to 0. When printed it will print those default values 0.
Gaurav patil said:
1 decade ago
It is because the s1.SetData(10, 5.4f); will set the values if i, j but in line s1.Display(); function will call default constructor and default values assigned to i,j (0,0) .
That is why it will print 0 0.
That is why it will print 0 0.
Rajeev said:
1 decade ago
It is because the s1.SetData(10, 5.4f); will set the values if i, j but in line s1.Display(); function will call default constructor and default values assigned to i,j (0,0) . that is why it will print 0 0
IldaOkan said:
1 decade ago
Actually it works just like the "ref" and "out" type variables, when you type :
this.varialblename
The variable references to the variable in the constructor.
this.varialblename
The variable references to the variable in the constructor.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers