21

Possible Duplicate:
How do you give a C# Auto-Property a default value?

Is there any nice way to provide a default value for an automatic property?

public int HowHigh { get; set; } // defaults to 0

If not explicitly set anywhere, I want it to be 5. Do you know a simple way for it? E.g. I could set it in constructor or something, but that's not elegant.

UPDATE: C# 6 has got it: http://geekswithblogs.net/WinAZ/archive/2015/06/30/whatrsquos-new-in-c-6.0-auto-property-initializers.aspx

3

5 Answers 5

13

Best you can do is set it in the constructor, you cannot make changes within automatic properties, you will need a backing field and implement the setter/getter yourself otherwise.

Using a backing field you can write something like this:

private int _howHigh = 0;
public int HowHigh { get {return _howHigh; }  set { _howHigh = value; } }
1
  • I believe this is the best solution Commented Aug 2, 2011 at 21:19
13

No, there isn't any nice way of doing this - basically you have to set it in the constructor, which isn't pleasant.

There are various limitations to automatic properties like this - my biggest gripe is that there isn't a way to create a read-only automatic property which can be set in the constructor but nowhere else (and backed by a readonly field).

9
  • +1 for read-only except in constructors. I would kill for that given a certain design I want to implement.
    – Moo-Juice
    Commented Jan 14, 2011 at 14:12
  • 2
    May I propose a new Attribute for this in C# 5? DefaultValueAttribute would be perfect :)
    – Dercsár
    Commented Jan 14, 2011 at 14:13
  • 2
    @Dercsar: I don't think an attribute is the way to go here. I'd rather see a language change. After all, it's a piece of language syntactic sugar. I highly doubt we'll see it in C# 5 though.
    – Jon Skeet
    Commented Jan 14, 2011 at 14:14
  • "my biggest gripe" does having a private setter for the Property work for that?
    – hunter
    Commented Jan 14, 2011 at 14:21
  • 2
    @hunter: No, because that's not really read-only. It's still writable within the class... and the backing variable will be writable too. Aside from anything else, it often makes sense to have a private setter if you want the class to be able to mutate the value. Your code should be able to easily declare that this property won't change. Currently you have to do that by declaring the variable separately, and a getter-only property :(
    – Jon Skeet
    Commented Jan 14, 2011 at 14:24
11

If the default value for the type is not sufficient, then the only way to do it is via a constructor.

0
3

In a word: No.

Automatic properties are a one trick pony, as soon as you need something extra (like a reasonable default value) you should revert to the backing field regular properties.

I'm a Resharper user, and it makes going from automatic to backed properties a breeze.

2

The constructor is NOT the only option you have.

I believe this is best:

private int m_HowHigh = 5; 
public int HowHigh { 
    get { return m_HowHigh; }  
    set { m_HowHigh = value; } 
} 

I prefer this for readability purposes more than the ctor().

This is NOT what you want:

[DefaultValue(5)]
public int HowHigh { get; set; }

Reference: http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx#Y2248

Because this is only a decoration and does not set the value (in C#4).

2
  • but this is not an auto property Commented Aug 29, 2016 at 14:12
  • In that case, the constructor was the only choice you had. Commented Oct 4, 2017 at 22:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.