字段
public class CalendarEntry { // private field private DateTime date; // public field (Generally not recommended.) public string day; // Public property exposes date field safely. public DateTime Date { get { return date; } set { // Set some reasonable boundaries for likely birth dates. if (value.Year > 1900 && value.Year <= DateTime.Today.Year) { date = value; } else throw new ArgumentOutOfRangeException(); } } // Public method also exposes date field safely. // Example call: birthday.SetDate("1975, 6, 30"); public void SetDate(string dateString) { DateTime dt = Convert.ToDateTime(dateString); // Set some reasonable boundaries for likely birth dates. if (dt.Year > 1900 && dt.Year <= DateTime.Today.Year) { date = dt; } else throw new ArgumentOutOfRangeException(); } public TimeSpan GetTimeSpan(string dateString) { DateTime dt = Convert.ToDateTime(dateString); if (dt != null && dt.Ticks < date.Ticks) { return date - dt; } else throw new ArgumentOutOfRangeException(); } }
CalendarEntry birthday = new CalendarEntry(); birthday.day = "Saturday";
public class CalendarDateWithInitialization { public string day = "Monday"; //... }
![]() |
---|
|
C# 语言规范
有关更多信息,请参见C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。