4

This must have been asked many times but I cannot find it....sorry...

Why is the following not permitted?

public string MyString = "initial value" {get; private set;}

(Visual C# Express 2010)

Mark H
  • 13,797
  • 4
  • 31
  • 45
Paulustrious
  • 609
  • 1
  • 11
  • 17
  • 1
    Unfortunate that is isn't possible to do presently - but it wouldn't be damaging to add the ability in future C# versions - I hope they do add it in future. (And add the ability to make readonly auto properties) – Mark H Jul 16 '10 at 15:03
  • 1
    It would be particularly nice if we could flag properties with automatic backing fields to make the backing field `readonly`. At that point, we could initialize through the property but only in the constructor. This has been discussed on SO before. – Steven Sudit Jul 16 '10 at 15:06
  • Finally, a feature where VB allows for a more concise syntax! In VB10, `Public Property MyString As String = "initial value"` works. – Heinzi Jul 16 '10 at 18:02
  • In VS2017 you can do exactly this: MyClass – Paulustrious Mar 31 '17 at 19:20
  • Possible duplicate of [How do you give a C# Auto-Property a default value?](https://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value) – Heretic Monkey Jun 13 '19 at 20:48

6 Answers6

5

It's just not valid syntax. You can't initialize the value of an auto-property, unfortunately.

The best options are to either make the property manually:

private string _MyString = "initial value";
public string MyString { get { return _MyString; } set { _MyString = value; } }

or initialize the value in the constructor:

public string MyString { get; set; }

....

public MyClass() {
    MyString = "initial value";
}
MisterZimbu
  • 2,673
  • 3
  • 27
  • 28
  • I understand there are various ways. I was just wondering why there wasn't a suitable valid syntax (yet). The problem with the above method is the declaration and initalisation are separated, possibly by many, many lines of code, I like @apollodude217 's syntax below – Paulustrious Jan 16 '17 at 20:04
2

An alternative:

string _strMyString;

public string MyString
{
    get {
        if (String.IsNullOrEmpty(_strMyString) {
             return "initial value";
        } else { 
             return _strMyString; 
        }
}
Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
1

It's a property, not a field. You can't initialize it this way. Just set the value in the constructor.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
1

the syntax

public string MyString { get; set; }

is replacing the old style / annoying / trivial (as of vs2008/c# 3.0 you can see all the new features of c# 3.0 here)

private string _MyString;

public string MyString 
{
    get { return _MyString; }
    set { _MyString = value; }
}

the compiler is actually generates a member before compiling your code. you can open a reflector and see the generated member.

Itai Zolberg
  • 108
  • 2
  • 7
1

Why?

I cannot speak on behalf of the designers of C#, but I can make educated speculation:

  1. They wanted to see just how big of a deal it is before taking the time to add another feature and yet another rule to the language.
  2. They could not find a sufficiently elegant-looking way to do this.

That said, here is how I would allow values (when a set accessor is available, of course):

public string MyProp {get;set;} = "initial value"; // not valid C#

Without making the language any more complex, they could write the rule so that it applies to "[all] properties with set accessors" instead of to "default properties with set accessors":

// again, not valid C#:
public string MyProp
{
    get { return _MyProp;}
    set { _MyProp = value; }
} = "initial value before being massaged or rejected by the set accessor.";

The only downside I see here is that it is ugly. The benefits are that you can concisely specify an initial value for a property with that property instead of in the constructor, and that you can let the value be massaged / checked / whatever via constructor at runtime if you wish.

jyoungdev
  • 2,674
  • 4
  • 26
  • 36
1

In VS 2017 you can:

    public int Minimum { get; private set; } = 0;
    public int Maximum { get; set; } = 5;
Paulustrious
  • 609
  • 1
  • 11
  • 17