0

Why these 2 errors and how to solve them?

enter image description here

Code:

    public static class EnumExtension
    {
        public static T EnumFlagsAll<T>(this T myEnum) where T : Enum
        {
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
            T result = default(T);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.

            result = (T)0;

            foreach (T val in Enum.GetValues(typeof(T)))
            {
               result = result | val;    
            }

            return result;
        }
    }

Also, can we remove the warning and how?

By the way: Please use new project setting: Nullable => Enable

enter image description here

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • Don't forget that you need to use `where T : struct, Enum` to properly constrain `T` to an actual enum. without the `struct,` part, `T` could be `System.Enum` itself. 1) Any actual enum value will be boxed, because `System.Enum` is a reference type (surprise!) and 2) if `T` can be `System.Enum`, it has no underlying type yet, so there can be no conversion from `0`. Does that change you you perceive what's going on there? – madreflection Apr 28 '23 at 18:39

0 Answers0