Skip to content

Latest commit

 

History

History
24 lines (14 loc) · 1.3 KB

generic-constraints.md

File metadata and controls

24 lines (14 loc) · 1.3 KB

Enhanced generic constraints

some of the restrictions on generic constraints have been removed. You can now specify the type System.Enum or System.Delegate as base class constraints for a type parameter.

You can also use the new unmanaged constraint, to specify that a type parameter must be an unmanaged type. An unmanaged type is a type that isn't a reference type and doesn't contain any reference type at any level of nesting.

Consider the following generic method that builds a dictionary of all the values in an enum mapped to the string representations:

Next, declare an enum type:

Finally, test how it works:

Modify the enum values, or the method declaration or test code to experiment on your own.

Previous: Local functions « Home: Home