Lyly
Lyly

Reputation: 726

How to change the default value of a property?

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?

Upvotes: 0

Views: 476

Answers (1)

svick
svick

Reputation: 244757

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.

Upvotes: 2

Related Questions