locked
Is it good practice to use this keyword in Properties? RRS feed

  • Question

  • User1820827484 posted

    Hello Community, 

    Following is code.  I am pasting to get good advice.

    public class Bug
        {
            
            private int bugID;
            private string title;
            private string description;
            private bool status;
            private DateTime modifiedDate;
            private DateTime fixedDate;
    
            public int BugID {
                
                set { this.bugID = value; }
                get { return this.bugID; }
    
            }
    
            public string Title {
    
                set { this.title = value; }
                get { return this.title; }
    
             }
    
            public string Description
            {
    
                set { this.description = value; }
                get { return this.description; }
    
            }
    
            public bool Status
            {
    
                set { this.status = value; }
                get { return this.status; }
    
            }
    
            public DateTime ModifiedDate
            {
    
                set { this.modifiedDate = value; }
                get { return this.modifiedDate; }
    
            }
    
            public DateTime FixedDate
            {
    
                set { this.fixedDate = value; }
                get { return this.fixedDate; }
    
            }
    
            // default constructor
    
            public Bug() { 
                
                this.bugID = -1;
                this.title = string.Empty;
                this.description = string.Empty;
                this.status = false;
                this.modifiedDate = DateTime.Now;
                this.ModifiedDate = DateTime.Now;
    
            }
    
    
    
    
        }



    Is it fine to use this keyword in Properties?

    Sunday, August 18, 2013 3:41 PM

Answers

  • User-183374066 posted

    Is it fine to use this keyword in Properties?

    yes but not necessary. 

    Use auto property instead

    public class Bug
    {
        public int BugID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool Status { get; set; }
        public DateTime ModifiedDate  { get; set; }
        public DateTime FixedDate  { get; set; }
    
        public Bug()
        {
            BugID = -1;
            Title = string.Empty;
            Description = string.Empty;
            ModifiedDate = DateTime.Now;
            FixedDate = DateTime.Now;
        }
    }

    status = false;

    No need to set bool value to false .. bool's default value is false 

    See Default Values Table

    title = string.Empty;

    It is ok. Default value for all the reference types are null. As string is reference type so it's default value is null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 4:24 PM
  • User281315223 posted

    Nassar's suggestion is practical for both public and private (or any other access-modifers) properties and is a fairly common practice.

    What he means by "auto" property is actually using the default value for a specific data-type instead of explicitly setting it within the constructor of your class. 

    For instance in your default constructor, your are explicitly setting all of these values : 

    public Bug() {
           this.bugID = -1;
           this.title = string.Empty;
           this.description = string.Empty;
           this.status = false;
           this.modifiedDate = DateTime.Now;
           this.ModifiedDate = DateTime.Now;
    }

    which is perfectly fine. However as MetalAsp.Net noted, the "this" keyword within this context isn't really necessary and you could easily use the following instead : 

    public Bug() {
           bugID = -1;
           title = string.Empty;
           description = string.Empty;
           status = false;
           modifiedDate = DateTime.Now;
           ModifiedDate = DateTime.Now;
    }

    In your case, Nassar's suggestion of using the "auto" properties (or default values) is only going to work for your status property as it is set to the default value of a boolean (false) but all of your other properties would need to be set to the values that you are specifying.

    All of these options are completely valid and it is ultimately up to your and your personal perference when deciding which you like best.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 5:25 PM
  • User2130758966 posted

    Use auto property instead

    If you do decide to use auto properties then you can insert them quickly by typing the following:

    prop

    then press the tab key twice.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 6:24 PM
  • User1109032460 posted

    What he means by "auto" property is actually using the default value

    "auto" properties (or default values)

    I'm not sure he means that at all. I think he means using C#'s auto properties, which just means that the compiler generates the backing field and you can use the terse syntax of

    public string Foo { get; set; }

    rather than have to declare both the field and the getter/setter logic.

    The obvious side-effect of using auto properties is that the backing field will inevitably be initialised to the default value for its type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2013 3:48 AM

All replies

  • User-1360095595 posted

    It is fine, yes, but unnecessary. I only the this keyword when necessary for disambiguation. 

    Sunday, August 18, 2013 3:44 PM
  • User-183374066 posted

    Is it fine to use this keyword in Properties?

    yes but not necessary. 

    Use auto property instead

    public class Bug
    {
        public int BugID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool Status { get; set; }
        public DateTime ModifiedDate  { get; set; }
        public DateTime FixedDate  { get; set; }
    
        public Bug()
        {
            BugID = -1;
            Title = string.Empty;
            Description = string.Empty;
            ModifiedDate = DateTime.Now;
            FixedDate = DateTime.Now;
        }
    }

    status = false;

    No need to set bool value to false .. bool's default value is false 

    See Default Values Table

    title = string.Empty;

    It is ok. Default value for all the reference types are null. As string is reference type so it's default value is null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 4:24 PM
  • User1820827484 posted

    Hello Nasser Malik,

    Thanks for help.

    Can you please explain more about Auto Property because in this case how to handle private variables? 

    In this case all variable are public  ? 

    Sunday, August 18, 2013 4:40 PM
  • User281315223 posted

    Nassar's suggestion is practical for both public and private (or any other access-modifers) properties and is a fairly common practice.

    What he means by "auto" property is actually using the default value for a specific data-type instead of explicitly setting it within the constructor of your class. 

    For instance in your default constructor, your are explicitly setting all of these values : 

    public Bug() {
           this.bugID = -1;
           this.title = string.Empty;
           this.description = string.Empty;
           this.status = false;
           this.modifiedDate = DateTime.Now;
           this.ModifiedDate = DateTime.Now;
    }

    which is perfectly fine. However as MetalAsp.Net noted, the "this" keyword within this context isn't really necessary and you could easily use the following instead : 

    public Bug() {
           bugID = -1;
           title = string.Empty;
           description = string.Empty;
           status = false;
           modifiedDate = DateTime.Now;
           ModifiedDate = DateTime.Now;
    }

    In your case, Nassar's suggestion of using the "auto" properties (or default values) is only going to work for your status property as it is set to the default value of a boolean (false) but all of your other properties would need to be set to the values that you are specifying.

    All of these options are completely valid and it is ultimately up to your and your personal perference when deciding which you like best.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 5:25 PM
  • User2130758966 posted

    Use auto property instead

    If you do decide to use auto properties then you can insert them quickly by typing the following:

    prop

    then press the tab key twice.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 18, 2013 6:24 PM
  • User1109032460 posted

    What he means by "auto" property is actually using the default value

    "auto" properties (or default values)

    I'm not sure he means that at all. I think he means using C#'s auto properties, which just means that the compiler generates the backing field and you can use the terse syntax of

    public string Foo { get; set; }

    rather than have to declare both the field and the getter/setter logic.

    The obvious side-effect of using auto properties is that the backing field will inevitably be initialised to the default value for its type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 19, 2013 3:48 AM