-
#nullable enable
public class Bar {
public void Foo(IDictionary<string, Bar?> lookup) {
//...
}
public void Test(IList<Bar> someList) {
var lookup = someList.ToDictionary(x => x.Id);
Foo(lookup); // CS8620
}
} This sucks. Clearly Instead the best solution I've come up with is: public static class IgnoreNullableWarning
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDictionary<string, T?> FixNullable<T>(this IDictionary<string, T> input)
where T: class
{
#nullable disable
return input;
#nullable enable
}
} Other alternatives: var lookup = someList.ToDictionary<Bar?, string>(x => x!.Id); #nullable disable
Foo(lookup);
#nullable enable Is the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I think that's exactly the right behavior. Consider this modification to your code: public void Foo(IDictionary<string, Bar?> lookup) {
lookup.Add("foo", null);
} Now in Also, instead of all those |
Beta Was this translation helpful? Give feedback.
-
What might be useful here is target typed Nullable inference |
Beta Was this translation helpful? Give feedback.
-
Other options:
|
Beta Was this translation helpful? Give feedback.
I think that's exactly the right behavior. Consider this modification to your code:
Now in
Test
, you haveDictionary<string, Bar>
which can give younull
Bar
, so there should be a warning somewhere and I think where the compiler puts it now is the right place for it.Also, instead of all those
#nullable disable
/enable
, you can just use!
.