c# - Restrict generic T to specific types -
i have following method:
public static iresult<t, string> validate<t>(this t value) { } // validate
how can restrict t int16, int32, int64, double , string?
you can't restrict generics in way, can choose single class constraint. must either make 5 overloads or find interface 5 of things share , use that. option choose depend on validate
doe.
here how overloads.
public static iresult<int16, string> validate<t>(this int16 value) { } // validate public static iresult<int32, string> validate<t>(this int32 value) { } // validate public static iresult<int54, string> validate<t>(this int64 value) { } // validate public static iresult<double, string> validate<t>(this double value) { } // validate public static iresult<string, string> validate<t>(this string value) { } // validate
here using common interface, of members list implement iconvertible
restrict that, allow iconvertible not 5 listed.
public static iresult<t, string> validate<t>(this t value) iconvertible { } // validate
Comments
Post a Comment