Within a class if you have methods and properties you can refer to properties declared in the same class
as this.property or property.
What is the best practice way?
Is there any performance impact?
"The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method."
The above article discusses common uses of this.
One example they give, you can not avoid this:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
damon2012, a code example from you would add context to your question.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
That's the type of example I am talking about. I do not have the real example with me.
Yes so when you happen to have parameter names the same as property names in the same class then you have to use the keyword this.
But this may be not a common occurrence. The question is when you do not have to use the keyword this, should you by best practice use it when you are setting the properties to values from within methods?
Given that SetValues and SetValuesWithThis result in the same IL, it appears that it's a matter of taste.
FWIW, my personal preference would be to not use this when it is not required. YMMV
What's most important with regards to code in general is that it's clear to read and robust. An argument could also be made for always using this.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
Member
70 Points
435 Posts
Whats best practice for referencing properties in same class?
Feb 06, 2015 03:28 PM|damon2012|LINK
as this.property or property.
What is the best practice way?
Is there any performance impact?
Star
14297 Points
5797 Posts
Re: Whats best practice for referencing properties in same class?
Feb 06, 2015 04:30 PM|gerrylowry|LINK
@damon2012
Perhaps you can show a code example to clarify?
https://msdn.microsoft.com/en-us/library/dk1507sz.aspx "this (C# Reference)"
"The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method."
The above article discusses common uses of this.
One example they give, you can not avoid this:
damon2012, a code example from you would add context to your question.
Member
70 Points
435 Posts
Re: Whats best practice for referencing properties in same class?
Feb 06, 2015 05:04 PM|damon2012|LINK
That's the type of example I am talking about. I do not have the real example with me.
Yes so when you happen to have parameter names the same as property names in the same class then you have to use the keyword this.
But this may be not a common occurrence. The question is when you do not have to use the keyword this, should you by best practice use it when you are setting the properties to values from within methods?
public string setValues()
{
this.name = "";
this.house = "";
this.animal = "";
}
public string name {get; set;}
public string house {get; set;}
public string animal {get; set;}
Star
14297 Points
5797 Posts
Re: Whats best practice for referencing properties in same class?
Feb 06, 2015 07:23 PM|gerrylowry|LINK
@damon2012
i'm going to go out on a limb and suggest it does not matter when the results are identical. i've modified your example slightly to set it to void:
IL:
IL:
Given that SetValues and SetValuesWithThis result in the same IL, it appears that it's a matter of taste.
FWIW, my personal preference would be to not use this when it is not required. YMMV
What's most important with regards to code in general is that it's clear to read and robust. An argument could also be made for always using this.
Member
70 Points
435 Posts
Re: Whats best practice for referencing properties in same class?
Feb 07, 2015 02:20 AM|damon2012|LINK
Thanks, that's a good answer.