/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/NullForgivingOperator.cs
64 строки
1 KB
Bill Wagner
first draft of nameof updates (#29549)
15 июн 2022, 00:43
Не верифицирован
15 июн 2022, 00:43
86ba43a
Код
Авторство
О чём код?
using System.Diagnostics.CodeAnalysis; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace operators; // <SnippetPersonClass> #nullable enable public class Person { public Person(string name) => Name = name ?? throw new ArgumentNullException(nameof(name)); public string Name { get; } } // </SnippetPersonClass> [TestClass] public class PersonTests { // <SnippetTestPerson> [TestMethod, ExpectedException(typeof(ArgumentNullException))] public void NullNameShouldThrowTest() { var person = new Person(null!); } // </SnippetTestPerson> } public static class UseNullForgivingExample { // <SnippetUseNullForgiving> public static void Main() { Person? p = Find("John"); if (IsValid(p)) { Console.WriteLine($"Found {p!.Name}"); } } public static bool IsValid(Person? person) => person is not null && person.Name is not null; // </SnippetUseNullForgiving> public static Person? Find(string name) => null; } public static class UseAttributeExample { // <SnippetUseAttribute> public static void Main() { Person? p = Find("John"); if (IsValid(p)) { Console.WriteLine($"Found {p.Name}"); } } public static bool IsValid([NotNullWhen(true)] Person? person) => person is not null && person.Name is not null; // </SnippetUseAttribute> public static Person? Find(string name) => null; }