Add samples of the expected behavior to the 2019-9-11 notes
This commit is contained in:
parent
0489cb64b7
commit
c33989610b
|
@ -144,7 +144,78 @@ T M<T>()
|
|||
|
||||
Let's implement the "state after assignment" rule as defined and implement the
|
||||
"Alternative" proposal outlined above. We will consider updating to use the
|
||||
"three state domain" above later, which may have some further changes.
|
||||
"three state domain" above later, which may have some further changes. A sample
|
||||
of the expected behavior follows:
|
||||
|
||||
Non-Generic
|
||||
|
||||
```C#
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
class Widget {
|
||||
string _description = string.Empty;
|
||||
|
||||
[AllowNull]
|
||||
string Description {
|
||||
get => _description;
|
||||
set => _description = value ?? string.Empty;
|
||||
}
|
||||
|
||||
static void Test(Widget w) {
|
||||
w.Description = null; // ok
|
||||
Console.WriteLine(w.Description.ToUpper()); // ok
|
||||
|
||||
if (w.Description == null) {
|
||||
Console.WriteLine(w.Description.ToUpper()); // warning
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Generic
|
||||
|
||||
```C#
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
class Box<T> {
|
||||
T _value;
|
||||
|
||||
[AllowNull]
|
||||
T Value {
|
||||
get => _value;
|
||||
set {
|
||||
if (value != null) {
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void TestConstrained<U>(Box<U> box) where U : class {
|
||||
box.Value = null; // ok
|
||||
Console.WriteLine(box.Value.ToString()); // ok
|
||||
|
||||
if (box.Value == null) {
|
||||
Console.WriteLine(box.Value.ToString()); // warning
|
||||
}
|
||||
}
|
||||
|
||||
static void TestUnconstrained<U>(Box<U> box, U value) {
|
||||
box.Value = default(U); // 'default(U)' always produces a warning when U could be a non-nullable reference type
|
||||
Console.WriteLine(box.Value.ToString()); // ok
|
||||
|
||||
box.Value = value; // ok
|
||||
Console.WriteLine(box.Value.ToString()); // ok
|
||||
|
||||
if (box.Value == null) {
|
||||
Console.WriteLine(box.Value.ToString()); // warning
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## More triage
|
||||
|
|
Loading…
Reference in a new issue