Add samples of the expected behavior to the 2019-9-11 notes

This commit is contained in:
Andy Gocke 2019-09-12 01:01:23 -07:00 committed by GitHub
parent 0489cb64b7
commit c33989610b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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