MCQs>IT & Programming>C#>Which of the following is the best way of creating a generic method constraining T to an Enum?
C# MCQs
Which of the following is the best way of creating a generic method constraining T to an Enum?
Answer
Correct Answer: public static class EnumUtils { public static T ParseEnum<T>(string value, T defaultValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; } }
Explanation:
Note: This Question is unanswered, help us to find answer for this one