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

Community
  • 1
  • 1
Dercsár
  • 1,636
  • 2
  • 14
  • 26

5 Answers5

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; } }
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
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).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 for read-only except in constructors. I would kill for that given a certain design I want to implement. – Moo-Juice Jan 14 '11 at 14:12
  • 2
    May I propose a new Attribute for this in C# 5? DefaultValueAttribute would be perfect :) – Dercsár Jan 14 '11 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 Jan 14 '11 at 14:14
  • "my biggest gripe" does having a private setter for the Property work for that? – hunter Jan 14 '11 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 Jan 14 '11 at 14:24
  • Perhaps there is a comparison here between automatically implemented properties and support for iterator blocks. VB.NET was late to the game in implementing both (with iterator block support only forthcoming in the async CTP) but arguably superior (http://stackoverflow.com/questions/4063275/iterators-in-vb-net-vnext-and-limitations-of-iterators-in-c) implementation of one or both features. VB.NET does allow you to set a default value for automatic properties. I am not try to be a VB.NET troll here, just a language geek. Jon Skeet reads language specs. I read people who read language specs. – John Wigger Jan 14 '11 at 14:25
  • @John: Depending on the implementation, VB may well be doing a better job here. Do you know if it supports read-only automatic properties? I read C# and Java language specs, but haven't read the VB one :) – Jon Skeet Jan 14 '11 at 14:28
  • And as long as we are wishing for things, lets not forget extension properties :) – SWeko Jan 14 '11 at 14:35
  • @Jon, yes VB.NET does support read-only automatic properties. It is a good thing to have not read the VB.NET specs, there are some scary "features" left in for backwards compatibility to pre.NET VB. – John Wigger Jan 14 '11 at 15:00
11

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

Eric Olsson
  • 4,805
  • 32
  • 35
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.

SWeko
  • 30,434
  • 10
  • 71
  • 106
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).

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233