I have a project with <nullable>enable</nullable>
in the .csproj
and I am experiencing some weird behavior of the warnings.
I have a foreach statement that iterates over an enum, and foreach item in the enum runs some code. But VS2019 flags up the CS8605 "Unboxing possibly null value" warning when I try and do this.
Full code is shown here. The error displays over the deceleration of t
.
public static class Textures
{
private static readonly Dictionary<TextureSet, Texture2D> textureDict = new Dictionary<TextureSet, Texture2D>();
internal static void LoadContent(ContentManager contentManager)
{
foreach(TextureSet t in Enum.GetValues(typeof(TextureSet)))
{
textureDict.Add(t, contentManager.Load<Texture2D>(@"textures/" + t.ToString()));
}
}
public static Texture2D Map(TextureSet texture) => textureDict[texture];
}
I'm struggling to understand why there is potential for t
to be null as enums are non-nullable.
I'm wondering if, since Enum.GetValues
returns of type Array
if there is some implicit casting going on here that is the root of this problem.
My solution currently is just to suppress the warning. But I would like to understand what's going on here. Perhaps there is better way to iterate over an enum.