class Person
{
public string name;
public void SetName(string name)
{
name = name;
}
}
public void static Main(string[] args)
{
Person aPerson = new Person();
aPerson.SetName("ruby");
Console.WriteLine("person name is {0}}, aPerson.name);
}
The person's name is empty. This can eliminated using this.name = name;
What was the reason person name is empty(empty string)?
Currently, your assignment of
name = name;
is just assigning the value of the parameter back to itself. In other words, it's a no-op.
If you change it to:
this.name = name;
then that's assigning the value of the parameter to the field which is what you intended.
However, currently the Person's name
field won't refer to an empty string - it will be a null reference. There's a big difference between the two.
(Of course, normally you shouldn't have a public field, and you should use a property instead of a SetName
method.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With