#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    108
    Rep Power
    42

    [C#] Property Get Set, Default


    I'm updating some of my projects to use C# 3.0's automatic property enhancement. In 2.0, this is how I would declare and default a property.
    Code:
    public class TheClass
    {
        private string strProp = "default";
        public string StrProp
        {
           get { return strProp; }
           set {strProp = value; }
        }
    
        public TheClass()
        {
    
        }
    }
    And here is my 3.5 version, where do I set the default?

    Code:
    public class TheClass
    {
        public string StrProp { get; set; }
    
        public TheClass()
        {
    
        }
    }
    My first thought was to create a method called setDefaults.. in the method, set all of the class properties to the defaults I want.. and call that method at the start of each constructor. Is that correct?
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    If it's just a primitive or value type like string and you're not doing any extra work in your getter/setter, a property seems like overkill. You could just say public string StrProp = "default"; and leave it at that.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    108
    Rep Power
    42
    Originally Posted by f'lar
    If it's just a primitive or value type like string and you're not doing any extra work in your getter/setter, a property seems like overkill. You could just say public string StrProp = "default"; and leave it at that.
    Well I made a simple example, lets say that the set is private.. that makes my question more valid I guess.

    Code:
    public class TheClass
    {
        public string StrProp { get; private set; }
    
        public TheClass()
        {
    
        }
    }
    Now the property can be read by an outside class, but set internally. Whats the best way to set a default for this value?
  6. #4
  7. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    In the constructor.

    And the reason, f'lar, for not using a public field is for consistent encapsulation. You also can't define fields in interfaces, but can define properties.
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  8. #5
  9. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    Now that I had a moment to read more about automatic properties...

    This syntax...
    Code:
    public string MyString { get; set; }
    ...is just syntax sugar for this...
    Code:
    private string _unknown_field_name; //initialized to null
    public string MyString
    {
       get { return _unknown_field_name; }
       set { _unknown_field_name = value; }
    }
    No-one is forcing you to use automatic property syntax sugar.
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    108
    Rep Power
    42
    Originally Posted by LyonHaert
    No-one is forcing you to use automatic property syntax sugar.
    Correct, no one is - I was just double checking to make sure I wasn't missing some new built in sugar for default values.

    I've seen this in some other articles after a lot of searching, if you're using the automatic properties, and want to set default values: Make a method that sets up all of the property's default values, and call this method in each constructor. This way if you want to change the default value, it only needs to be done in one spot.
  12. #7
  13. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    Or just assign them in the constructor. Still just one spot. (Even if you have multiple constructors, because then you should be using constructor chaining.)
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0
    No-one is forcing you to use automatic property syntax sugar.
    That's very nice baby

    Comments on this post

    • Ronster disagrees : Woke up old post for a smart *** comment.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0
    Originally Posted by Kbalz
    My first thought was to create a method called setDefaults.. in the method, set all of the class properties to the defaults I want.. and call that method at the start of each constructor. Is that correct?
    Yupp.. constructors are good places to set default values..

IMN logo majestic logo threadwatch logo seochat tools logo