/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/UserDefinedConversions.cs
34 строки
766 B
Petr Kulikov
Moved C# operator snippets into the shared subfolder (#19843)
06 авг 2020, 17:07
Не верифицирован
06 авг 2020, 17:07
249bb88
Код
Авторство
О чём код?
using System; public readonly struct Digit { private readonly byte digit; public Digit(byte digit) { if (digit > 9) { throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine."); } this.digit = digit; } public static implicit operator byte(Digit d) => d.digit; public static explicit operator Digit(byte b) => new Digit(b); public override string ToString() => $"{digit}"; } public static class UserDefinedConversions { public static void Main() { var d = new Digit(7); byte number = d; Console.WriteLine(number); // output: 7 Digit digit = (Digit)number; Console.WriteLine(digit); // output: 7 } }