Enum fun... Why these 2 errors and how to solve them?

104 views Asked by At

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

0

There are 0 answers