Информация об изменениях

Сообщение Re[2]: C# 8 и null-допустимость - ошибка на миллиард от 16.05.2020 15:59

Изменено 16.05.2020 16:00 Serginio1

Re[2]: C# 8 и null-допустимость - ошибка на миллиард
Здравствуйте, Aquilaware, Вы писали:

A>Здравствуйте, Shmj, Вы писали:


S>>Пишут что сглупили, теперь хотят все исправить. Ваше мнение?


A>Исправление не получилось, так как оно использует кривую алгебру под капотом. В ней недостает понятия пустого значения ссылочного типа. Приведу пример:


A>
A>default(int?) = null
A>default(string?) = null
A>default(int) = 0
A>default(string) = ?
A>


A>Из-за этого, компилятор не знает чем инициализировать значения ненулевых ссылочных типов, т.к. в системе типов нет такого понятия. И пока компилятор не научится понимять что


https://docs.microsoft.com/en-us/dotnet/csharp/nullable-migration-strategies#late-initialized-properties-data-transfer-objects-and-nullability

Initialize the property to null

As a terser alternative to using a nullable backing field, or if the library that instantiates your class is not compatible with that approach, you can simply initialize the property to null directly, with the help of the null-forgiving operator (!):

[Required]
public string FirstName { get; set; } = null!;

[Required]
public string LastName { get; set; } = null!;

public string? VehicleRegistration { get; set; }

You will never observe an actual null value at runtime except as a result of a programming bug, by accessing the property before it has been properly initialized.

Re[2]: C# 8 и null-допустимость - ошибка на миллиард
Здравствуйте, Aquilaware, Вы писали:

A>Здравствуйте, Shmj, Вы писали:


S>>Пишут что сглупили, теперь хотят все исправить. Ваше мнение?


A>Исправление не получилось, так как оно использует кривую алгебру под капотом. В ней недостает понятия пустого значения ссылочного типа. Приведу пример:


A>
A>default(int?) = null
A>default(string?) = null
A>default(int) = 0
A>default(string) = ?
A>


A>Из-за этого, компилятор не знает чем инициализировать значения ненулевых ссылочных типов, т.к. в системе типов нет такого понятия. И пока компилятор не научится понимять что


https://docs.microsoft.com/en-us/dotnet/csharp/nullable-migration-strategies#late-initialized-properties-data-transfer-objects-and-nullability

Initialize the property to null

As a terser alternative to using a nullable backing field, or if the library that instantiates your class is not compatible with that approach, you can simply initialize the property to null directly, with the help of the null-forgiving operator (!):

[Required]
public string FirstName { get; set; } = null!;

[Required]
public string LastName { get; set; } = null!;

public string? VehicleRegistration { get; set; }

You will never observe an actual null value at runtime except as a result of a programming bug, by accessing the property before it has been properly initialized.



В конструкторах ты должен их заполнять.