0

In the past we declared classes and can change the default value of a property like this:

public class MyClass
{
    private string name;

    public string Name
    {
          get{  if(name==null) return "";  }
          set{  name= value; }
    }
}

Now we can do:

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

But how to change the default value in this way? For example, if the name is null, I want to get "" instead of null?

svick
  • 236,525
  • 50
  • 385
  • 514
Lyly
  • 718
  • 1
  • 8
  • 17
  • What stops you from doing the old school way as you do in version1? Suggested duplicate shows how to initialize Auto Property in constructor but note that is different from what you have shown here. – Sriram Sakthivel May 10 '14 at 12:54
  • 2
    I can't find a link to the blog stating so, but auto-implemented property initializers [are planned](http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2163145-allow-default-values-on-automatic-properties) for a future C# version, so you can do `public int X { get; set; } = x;`. – CodeCaster May 10 '14 at 12:56
  • What is different,please? – Lyly May 10 '14 at 12:56
  • I am using vs2012 and my .netframework version is 4.0,I think c#6.0 is not suitable for me. – Lyly May 10 '14 at 12:58
  • In your code you doesn't set `name` field to empty string. you just return empty string when property is called (underlying field will be still `null`). where as with auto properties you can't do that. You need to initialize the field with empty string. That is the difference. – Sriram Sakthivel May 10 '14 at 13:01
  • 2
    There's another slight difference. If the user of MyClass sets Name explicitly to null, the first example would still return String.Empty. Simply initializing the property to a value wouldn't have this behavior. Whether that matters depends upon the expected behavior of Name. – KevinS May 10 '14 at 13:31
  • @FarhadJabiyev It's not a duplicate, that question is “how to set the value of a property before it has been set for the first time?” This question is “how to replace `null` value with some specified value”? – svick May 10 '14 at 13:36
  • 1
    @CodeCaster One place the _auto-property initializers_ are mentioned is [Roslyn: Language feature implementation status](https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status). – Jeppe Stig Nielsen May 10 '14 at 13:39
  • @svick Got it Retracted. – Farhad Jabiyev May 10 '14 at 13:43

1 Answers1

2

Automatic properties are useful when you don't need any additional logic in the property, they don't make the full syntax obsolete. So, the first sample is exactly what you should be doing, there is no simpler way to do it.

svick
  • 236,525
  • 50
  • 385
  • 514
  • @Selman22 The proposed version of C# 6.0 will help with the situation in the “possible duplicate” question, but not with this. – svick May 10 '14 at 13:41
  • May be I can do it in the constructor.I has the same result. – Lyly May 10 '14 at 13:42
  • @Lyly I thought that if you reset `Name` to `null` and then read it again, you want it to return `""`. Constructor won't help you with that. – svick May 10 '14 at 13:43
  • @svick Yes,I get your meaning.Using constructor is only for one time. – Lyly May 10 '14 at 13:45
  • @svick Do you mean that the first sample I write and using constructor is different way? – Lyly May 10 '14 at 13:50
  • @Lyly Yeah, I mean that sample you wrote. Like I said before, I don't think that setting the value in the constructor accomplishes what you're asking for. – svick May 10 '14 at 13:55