/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/programming-guide/classes-and-structs/snippets/fields/Program.cs
82 строки
2 KB
Tom Dykstra
Update snippets for NRT (#28622)
11 мар 2022, 19:03
Не верифицирован
11 мар 2022, 19:03
13fe3ed
Код
Авторство
О чём код?
//<Snippet1> public class CalendarEntry { // private field (Located near wrapping "Date" property). private DateTime _date; // 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("Date"); } } } // public field (Generally not recommended). public string? Day; // 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("dateString"); } } public TimeSpan GetTimeSpan(string dateString) { DateTime dt = Convert.ToDateTime(dateString); if (dt.Ticks < _date.Ticks) { return _date - dt; } else { throw new ArgumentOutOfRangeException("dateString"); } } } //</Snippet1> class TestCalendarDate { static void Main() { //<Snippet2> CalendarEntry birthday = new CalendarEntry(); birthday.Day = "Saturday"; //</Snippet2> } } //<Snippet3> public class CalendarDateWithInitialization { public string Day = "Monday"; //... } //</Snippet3>