/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/compiler-messages/snippets/null-warnings/PersonExamples.cs
99 строк
2 KB
Bill Wagner
Cover all nullable warnings (#30045)
01 июл 2022, 23:54
Не верифицирован
01 июл 2022, 23:54
8bce28a
Код
Авторство
О чём код?
namespace firstExample { // <PersonExample> public class Person { public string FirstName { get; set; } public string LastName { get; set; } } // </PersonExample> } namespace secondExample { // <WithConstructor> public class Person { public Person(string first, string last) { FirstName = first; LastName = last; } public string FirstName { get; set; } public string LastName { get; set; } } // </WithConstructor> } namespace thirdExample { // <Initializer> public class Person { public string FirstName { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty; } // </Initializer> } namespace fourthExample { // <NullableMember> public class Person { public string? FirstName { get; set; } public string? LastName { get; set; } } // </NullableMember> } namespace initializationExample { // <ConstructorChainingAndMemberNotNull> using System.Diagnostics.CodeAnalysis; public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public Person() : this("John", "Doe") { } } public class Student : Person { public string Major { get; set; } public Student(string firstName, string lastName, string major) : base(firstName, lastName) { SetMajor(major); } public Student(string firstName, string lastName) : base(firstName, lastName) { SetMajor(); } public Student() { SetMajor(); } [MemberNotNull(nameof(Major))] private void SetMajor(string? major = default) { Major = major ?? "Undeclared"; } } // </ConstructorChainingAndMemberNotNull> }