私はこのクラスを使用して別のクラスのList型のコレクションになるプロパティを持つC#クラスを持っています。C#クラスのプロパティを常に特定のデフォルト値に設定することはできますか?
私は何をしたいことがちょうど常に値の「3」
するTypeプロパティを設定されているが/これはゲッター/セッターで行うことができべきか、私はSystem.Component.DefaultValueを使用する必要があります。.. ..私は、私はこの方法を使用しないことを好むだろうと考えて
public class ReportDefinition
{
public int Id { get; set; }
public string ReportGroupNameDef { get; set; }
public int SortOrder { get; set; }
public int ReportGroupId { get; set; }
[System.ComponentModel.DefaultValue(3)]
public int Type { get; set; }
}
属性[System.ComponentModel.DefaultValue(3)]
[C#6以上(http://stackoverflow.com/questions/40730/how-do-you-give-ac-sharp-auto-property-a-default-value)場合: 'public int Type {get;セット; } = 3; ' – Quantic
C#6は.net 4.5ですか? –
まあ、私のLinqpadはそれについて文句を言う... Linqpad 4 ....どのような他の方法? –