I have some interface and class implementing that interface:
public interface IWhatever {
bool Value { get; set;}
}
public class Whatever : IWhatever {
public bool Value { get; set; }
}
Now, does C#
allow the Value
to have some default value without using some backing field?
Update
As of C# 6 (VS2015) this syntax is perfectly valid
public bool Value { get; set; } = true;
as is setting a value for a readonly property
public bool Value { get; } = true;
The old, pre C# 6 answer
Spoiler alert for those of an excitable nature: The following code will not work
Are you asking, "Can I do this?"
public bool Value { get; set; } = true;
No, you can't. You need to set the default value in the constructor of the class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With