문제

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

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

     public string Reason{ get; set; }
}
도움이 되었습니까?

해결책 2

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; }
}

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top