Is there a generic constructor with parameter constraint in C#?

In C# you can put a constraint on a generic method like: public class A { public static void Method<T> (T a) where T : new() { //…do something… } } Where you specify that T should have a constructor that requires no parameters. I’m wondering whether there is a way to add a constraint … Read more

Create Generic method constraining T to an Enum

I’m building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) … Read more