c# - Inverting a generic constraint -


this question has answer here:

i've been heavily learning generic constraints last week. while learning them more in depth, became curious if it's possible invert them in way. people work aren't sure such thing exists , google searching has come nothing.

the closest answer got use struct instead of class, while can see why person thought decent answer , work of uses, still doesn't answer if there way of inverting constraint.

does else know if it's possible invert constraint?

e.g.

class myclass<t> t: !class { } 

no, there no such syntax in c#.

from msdn:

the following table lists 6 types of constraints:

where t: struct type argument must value type. value type except nullable can specified. see using nullable types more information. t : class type argument must reference type; applies class, interface, delegate, or array type.

where t : new() type argument must have public parameterless constructor. when used other constraints, new() constraint must specified last.

where t : (base class name) type argument must or derive specified base class.

where t : (interface name) type argument must or implement specified interface. multiple interface constraints can specified. constraining interface can generic.

where t : u type argument supplied t must or derive argument supplied u.

so there no option where t type other u

think way - suppose had

class myclass<t> t: !string { } 

you know t not string, you have no other indication of t might be. how code against it? these valid delcarations:

var x1 = new myclass<int>(); var x2 = new myclass<object>(); var x3 = new myclass<datetime>(); var x4 = new myclass<datatable>(); 

what code have applies of these types, invalid string?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -