VS2008 (자동 속성)에서 "짧은 스타일"속성을 사용하여 기본값을 설정하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/206611

문제

다음과 같이 정의 된 속성에 기본값을 설정하려면 어떻게해야합니까?

public int MyProperty { get; set; }

VS2008 (코드 스 니펫)에서 "prop[탭] [탭]을 사용하고 있습니다.

"오래된 길"으로 돌아 가지 않고 가능합니까? :

private int myProperty = 0; // default value
public int MyProperty
{
    get { return myProperty; }
    set { myProperty = value; }
}

시간 내 줘서 고마워. 친애하는.

도움이 되었습니까?

해결책

생성자 내에서 "기본"값을 설정하면됩니다.

public class Person
{
   public Person()
   {
       this.FirstName = string.Empty;
   }

   public string FirstName { get; set; }
}

또한 자동 속성이라고합니다.

다른 팁

My preference would be to do things "the old way", rather than init in the constructor. If you later add another constructor you'll have to be sure to call the first one from it, or your properties will be uninitialized.

Even though this is an older Thread i'd like to add some new way implemented in C# > Vers. 6:

https://msdn.microsoft.com/de-de/library/bb384054.aspx

public int MyInt { get; set; } = 0;
public string MyString { get; set; } = "Lorem Ipsum";

[DefaultValue("MyFirstName")] public string FirstName { get; set; }

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