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 2 of 3.
Snehal said:
1 decade ago
0 0 are default values set by constructor when object gets created.
Snehal said:
1 decade ago
Assignment should done to the different variables.
That's why it is printing default var 0 0?
That's why it is printing default var 0 0?
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.
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
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);
}
}
Chowdaiah said:
1 decade ago
Here don't use this keyword that's why does not takes values for example if u take this.i and this.j in setdata then u will print some values so output is 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.
Ajesh said:
1 decade ago
If you run this program using "this" keyword the answer becomes 10 and 5.4 and them how come zero?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers