Код: Выделить всё
public class Person
{
public Person(string name) => Name = name;
public string Name { get; }
public static implicit operator string(Person person) => person.Name;
}
< /code>
и этот тестовый пример: < /p>
public class Program
{
public static void Main()
{
var person = new Person("John");
// works
string? name1 = true ? person : null;
// CS8604 Possible null reference argument for parameter 'name' in 'Person.implicit operator string(Person name)'.
// also NullReferenceException
string? name2 = false ? person : null;
}
}
< /code>
Это можно увидеть в этом запущенном резо. Переменная человек Подробнее здесь: https://stackoverflow.com/questions/796 ... tor-method