![]() By using the null-forgiving operator, you inform the compiler that passing null is expected and shouldn't be warned about. Without the null-forgiving operator, the compiler generates the following warning for the preceding code: Warning CS8625: Cannot convert null literal to non-nullable reference type. ![]() Using the MSTest test framework, you can create the following test for the validation logic in the constructor: Public Person(string name) => Name = name ? throw new ArgumentNullException(nameof(name)) ![]() For example, consider the following class: #nullable enable One of the use cases of the null-forgiving operator is in testing the argument validation logic. At run time, expression x! evaluates to the result of the underlying expression x.įor more information about the nullable reference types feature, see Nullable reference types. It only affects the compiler's static flow analysis by changing the null state of the expression. The null-forgiving operator has no effect at run time. The unary prefix ! operator is the logical negation operator. In an enabled nullable annotation context, you use the null-forgiving operator to declare that expression x of a reference type isn't null: x!. ![]() The unary postfix ! operator is the null-forgiving, or null-suppression, operator. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |