locked
Whats best practice for referencing properties in same class? RRS feed

  • Question

  • User-257409430 posted
    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?
    Friday, February 6, 2015 3:28 PM

Answers

  • User-434868552 posted

    @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:

    public class Damon2012
    {
        public string name {get; set;}
        public void SetValues()
        {
            name  = "";
        }
    }

    IL:

    Damon2012.SetValues:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  ldstr       ""
    IL_0007:  call        UserQuery+Damon2012.set_name
    IL_000C:  nop         
    IL_000D:  ret 
    public class Damon2012
    {
        public string name {get; set;}
        public void SetValuesWithThis()
        {
            this.name  = "";
        }
    }

    IL:

    Damon2012.SetValuesWithThis:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  ldstr       ""
    IL_0007:  call        UserQuery+Damon2012.set_name
    IL_000C:  nop         
    IL_000D:  ret         

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 7:23 PM

All replies

  • User-434868552 posted

    @damon2012

    Within a class if you have methods and properties you can refer to properties declared in the same class
    as this.property or property.

    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

    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.

    Friday, February 6, 2015 4:30 PM
  • User-257409430 posted

    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;}

    Friday, February 6, 2015 5:04 PM
  • User-434868552 posted

    @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:

    public class Damon2012
    {
        public string name {get; set;}
        public void SetValues()
        {
            name  = "";
        }
    }

    IL:

    Damon2012.SetValues:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  ldstr       ""
    IL_0007:  call        UserQuery+Damon2012.set_name
    IL_000C:  nop         
    IL_000D:  ret 
    public class Damon2012
    {
        public string name {get; set;}
        public void SetValuesWithThis()
        {
            this.name  = "";
        }
    }

    IL:

    Damon2012.SetValuesWithThis:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  ldstr       ""
    IL_0007:  call        UserQuery+Damon2012.set_name
    IL_000C:  nop         
    IL_000D:  ret         

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 7:23 PM
  • User-257409430 posted

    Thanks, that's a good answer.

    Saturday, February 7, 2015 2:20 AM