class Person
{
string name;
public Person(string name)
{
this.name = name;
}
public void method()
{
Person gupta = new Person("James"); // Current Object
Console.WriteLine(this.name);
Person gupta1 = new Person("Peter"); // Current Object
Console.WriteLine(this.name);
Person gupta2 = new Person("Frank"); // Current Object
Console.WriteLine(this.name);
}
static void Main(string[] args)
{
Person p = new Person("Jim");
p.method();
Console.ReadLine();
}
}
This code produced result
Jim
Jim
Jim
However if thought this should be
James
Peter
Frank
Can somebody explain please?
You are using
this
. Which refers to the object instance you are currently in. Change the code the following and you will get what you expect.When you call
the method() is being executed on the 'p' object... that is your 'current' object. Perhaps it is a poor choice of words.
'Current' reffers to 'the object that the method has been called on' it does not mean 'the last used object'
this
refers to the current instance. When in the Main method you create an instance of the Person class you are passingJim
to the constructor which is then stored in thename
field. Next you invoke the method. Inside this method you are creating multiple instances of thePerson
class :gupta
,gupta1
andgupta2
. You need to call thename
field on each instance:"Current object" is a rather informal way of speaking. It is ok as long as you don't get confused about what it refers to.
Do not think of it as a kind of "chronological" measure - the "current object" is not the very object you last instantiated anywere. Instead, it is the instance of the class on which you called the current execution of the method.
At the time of writing "this.(...)", you don't know which instance it will be. You will now when the code is called.
Inside this call to
method
, "this" will be Lucy - because the methodmethod
is being called on that particular instance of Person. The littlePerson
s you create insidemethod
will not affect this at all.Further clarification:
@Nadeem
You start the program by calling the main method, this creates a NEW Person with the name "Jim" and the variable p points to it.
You then call
So method() is being called from inside p. The method 'method()' makes 3 new Person objects with the names "James", "Peter" and "Frank" but the 'current scope' is still p. You are running 'method()' from inside p.
So if you ask p 'what is your name?', p will say, quite rightly 'Jim'.
When i was studying, I sometimes found it easier to think of it like this - if I have three children and then you ask me what my name is after the birth of each one, my name is still 5arx. You're not asking my children what their name is, you're asking me.
You could revise your code to make things a bit clearer (maybe!):
Hope this helps,
5arx
When you use
this.name
you are addressing the object itself and not the new objects that you created. (notice that you usedthis.name
three times and got three "Jims").So, if you want to print the names of the people you just created use
gupta.name
for the first one andgupta1.name
etc.other option is to hold a
List<Person>
and useforeach
to run on the list and print the names.Notice that there are many
Person
s that can live together in the same app. And a person object can hold otherperson
s objects. when you are inside a function of oneperson
(here its a function inside jim) thethis.name
will refer only to him (to jim) and not to the otherperson
s instances inside Jim!