18

Imagine you have a field _items in a class. You can initialize it at the point of declaration:

class C
{
  IList<string> _items=new List<string>();
}

Now I want to convert this field to an auto generated property, but the initialization is now invalid:

class C
{
  public IList<string> Items=new List<string>(); {get; set;} // Invalid
}

So, I have to do:

class C
{
  public IList<string> Items {get; set;}

  public C
  {
    Items=new List<string>();
  }
}

But this is not nearly as convenient as initializing fields at the point of declaration. Is there a better way to do this, without having to (needlessly) back this property with a private (initialized at the point of declaration) field, for example.

Thanks

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
  • 2
    possible duplicate of [How do you give a C# Auto-Property a default value?](http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value) – Justin Niessner Nov 01 '10 at 15:23

7 Answers7

24

No, automatic properties don't allow you to set an initial value.

It's annoying, but such is life. (It's annoying that they can't be readonly, too. But that's a rant for another day.)

EDIT: Both readonly automatically implemented properties and specifying an initial value are slated to be in C# 6.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It really is indeed. But let's look at it from the good side. We HAVE auto-get set. That alone makes me happy. – Proclyon Nov 01 '10 at 15:22
  • 1
    Yes, I would have *loved* being able to type `IList { get; private set; } = new List();` or something similar. – Fredrik Mörk Nov 01 '10 at 15:32
  • @Fredrik, that was exactly what I hoped for. Perhaps this would be a good feature to ask for in C# 5? – Michael Goldshteyn Nov 01 '10 at 15:47
  • @Michael: The C# team is aware that folks including myself would like a bit more control over automatic properties. I'm not personally holding out much hope for seeing it in C# 5, but async/await makes up for it :) – Jon Skeet Nov 01 '10 at 15:50
  • @T.J.Crowder: Yes, indeed it is... along with read-only automatically implemented properties, pretty much how I've requested them for ages :) – Jon Skeet Jul 15 '14 at 12:02
  • Found a citation: https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status Yay! – T.J. Crowder Jul 15 '14 at 12:05
  • @T.J.Crowder: Indeed. I'm not going to find and edit every old answer that mentions the state of play when I wrote it though... – Jon Skeet Jul 15 '14 at 12:09
  • @JonSkeet: No, of course not. :-) I just happened to notice. – T.J. Crowder Jul 15 '14 at 12:11
11

In c#6 you can write something like this:

class C
{
    public IList<string> Items {get; set;} = new List<string>();
}
Martin Slezák
  • 181
  • 2
  • 11
1

I don't believe so - on construction, as you demonstrate, is the next best way so you don't have to use a private backer, and then use constructor-chaining to ensure that the property always gets initialised:

public MyClass() { PropA = default_Value; }
public MyClass(params object[] args) : this() { /* other code */ }

It would be a nice feature; but then, auto implemented properties are there to simplify property implementation on types; and I can't see how you could add, at the language-level, the default-value feature to an auto-property without the code looking a little, well, odd.

But then, that's why I'm not a language designer.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

I'm afraid it is not possible to directly initialize an auto property when you declare it. And I agree that this is a pity. I don't know any better way then the constructor either. See also Initializing C# auto-properties

Community
  • 1
  • 1
treze
  • 3,159
  • 20
  • 21
0

Nope, this is the only way.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
0

Since you don't declare the field when you use { get; set; }, you can't specify the initial value.

So you need to declare the field yourself or initialize it in the constructor.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
0

Using a private backing field or setting the value in the constructor are the only ways to give initial values to a Property.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536