CSharp · Console
using System;
using System.Collections.Generic;
using System.Reflection;
#nullable enable
public class TestGNN<T>
{
public T this[string index] => default; // possible null ref that we'll ignore
}
public class TestGN<T>
{
public T? this[string index] => default;
}
public class TestNNS
{
public string this[string index] => index;
}
public class TestNS
{
public string? this[string index] => index;
}
public class TestNNI
{
public int this[string index] => 0;
}
public class TestNI
{
public int? this[string index] => null;
}
public class TestNNT
{
public Tuple<object, object> this[string index] => default; // possible null ref that we'll ignore
}
public class TestNT
{
public Tuple<object, object>? this[string index] => null;
}
public class Program
{
public static void Main()
{
List<object> cases =
[
new TestGNN<string>(),
new TestGNN<string?>(),
new TestGNN<int>(),
new TestGNN<int?>(),
new TestGNN<Tuple<object, object>>(),
new TestGNN<Tuple<object, object>?>(),
new TestGN<string>(),
new TestGN<string?>(),
new TestGN<int>(),
new TestGN<int?>(),
new TestGN<Tuple<object, object>>(),
new TestGN<Tuple<object, object>?>(),
new TestNNS(),
new TestNS(),
new TestNNI(),
new TestNI(),
new TestNNT(),
new TestNT()
];
NullabilityInfoContext context = new();
foreach (var c in cases)
{
var t = c.GetType();
foreach (var p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var info = context.Create(p);
Console.WriteLine($"{c}: {info.ReadState}");
}
}
}
}