6

What's the easiest/straight-forward way of setting a default value for a C# public property?

// how do I set a default for this?

public string MyProperty { get; set; }

Please don't suggest that I use a private property & implement the get/set public properties. Trying to keep this concise, and don't want to get into an argument about why that's so much better. Thanks.

FMFF
  • 1,652
  • 4
  • 32
  • 62
Alan
  • 2,574
  • 2
  • 18
  • 16
  • possible duplicate of http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value – devio Jul 26 '10 at 14:15
  • OT: I think you mean a private field – Douglas Jul 26 '10 at 14:16
  • I think you are confusing the concepts of public/private and automatic properties vs. manually-implemented properties with backing fields. These concepts are independent. – John Saunders Jul 26 '10 at 14:27

6 Answers6

9

Just initialize it in the constructor:

public class MyClass
{
    public string MyProperty { get; set; }

    public MyClass()
    {
        MyProperty = "default value";
    }
}

Note that if you have multiple constructors, you'll need to make sure that each of them either sets the property or delegates to another constructor which does.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Set the default in your constructor:

this.MyProperty = <DefaultValue>;
sbenderli
  • 3,654
  • 9
  • 35
  • 48
  • 1
    isn't the `this` statement redundant? – Jimmy Jul 26 '10 at 15:26
  • @Jimmy: I consider use of "this" in this situation to make it much more readable. I always want it to read . = . If it's just = then it breaks that pattern. – jcollum Feb 24 '11 at 17:10
3

Please don't suggest that I use a private property

That's the standard way to set such defaults. You might not like it, but that what even the automatic properties syntax does after compilation - it generates a private field and uses that in the getter and setter.

You can set the property in a constructor, which will be as close to a default as you can get.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • No argument there -- I'm just trying to keep non-critical code concise. – Alan Jul 26 '10 at 14:36
  • 2
    @Alan If you have more than one constructor then it will no longer be concise because you will have to start making sure that initialization occurs in all code paths. Using a private backing field you can have a single **concise** place to initialize that property. – AaronLS Jul 26 '10 at 15:07
  • 2
    @AaronLS you could probably be alright if you just chain your constructors. – Alan Jul 26 '10 at 17:43
2

No, it would be nice if you could just mark up the property to indicate the default value, but you can't. If you really want to use automatic properties, you can't set a default in property declaration.

The cleanest workaround I've come up with is to set the defaults in the constructor. It's still a little ugly. Defaults are not colocated with the property declaraion, and it can be a pain if you have multiple constructors, but that's still the best I've found

Mike Mooney
  • 11,729
  • 3
  • 36
  • 42
2

if you're going to use auto-implementation of properties, the only real option is to initialize the property values in the constructor(s).

Toby
  • 7,354
  • 3
  • 25
  • 26
1

As of C# 6.0 you can assign a default value to an auto-property.

public string MyProperty {get; set; } = "Default Value";
Michael
  • 810
  • 6
  • 18