c# - Why it is better to check type of T in generic class in static constructor instead of in non-static? -
my consideration why prefer doing in static constructor called once if given type incorrect in calls, not once , excpect exceptions in parts of code wrong type used.
this example of mean:
internal sealed class generictypethatrequiresanenum<t> { static generictypethatrequiresanenum() { if (!typeof(t).isenum) { throw new argumentexception("t must enumerated type"); } } }
why not non-static constructor?
because cannot use particular type @ if static constructor fails. so, there no point check type argument everywhere.
public class test<t> { static test() { throw new invalidoperationexception(); } }
usage:
new test<string>(); //throws typeinitializationexception
Comments
Post a Comment