簡體   English   中英

如何使用VS2008中的“短樣式”屬性設置默認值(自動屬性)?

[英]How to set a default value using “short style” properties in VS2008 (Automatic Properties)?

如何為定義如下的屬性設置默認值:

public int MyProperty { get; set; }

那就是在VS2008(代碼片段)中使用“prop”[tab] [tab]。

是否有可能不退回“舊方式”?:

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

此外,它們被稱為自動屬性。

我的偏好是做“舊方式”,而不是構造函數中的init。 如果您以后添加另一個構造函數,則必須確保從中調用第一個構造函數,否則您的屬性將被取消初始化。

即使這是一個較舊的線程,我想添加一些在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; 組; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM