Auto-property default value

1.5k views Asked by At

Can I generate in C# auto-property with default value?

public class MyClass
{
     MyClass()
     {
         Reason = "my reason";
     }

     public string Reason{ get; set; }
}
3

There are 3 answers

0
Abbas On BEST ANSWER

Yes, but to be able to create an instance from outside your class, make your constructor public.

public class MyClass
{
    public MyClass()
    {
        Reason = "my reason";
    }

    public string Reason {get; set; }
}
9
O. R. Mapper On

Yes, you can. Definitely. Just like you've shown it.

4
Antonio Petricca On

You have to add a default constructor and initialize the autoproperty value.