The "unmanaged" generic type constraint was added in c# 7.3, but I'm curious why a corresponding "managed" type constraint was not added at the same time (or since)?
The unmanaged constraint comes pretty close to being a constraint for primitive types, but will also constrain structs that do not refer to any managed types. That's fine, but I would like to write a constraint that is just the opposite. My immediate use case is to write extension methods for IDictionary<Object, Object> where the signatures look like:
public static void Add(this IDictionary<Object, Object> map, TValue value) where TValue : managed
and
public static bool TryGetValue(this IDictionary<Object, Object> map, out TValue value) where TValue : managed
Obviously the implementations would add and get values from the dictionary using the key: typeof(TValue).
Having such methods make sense (to me) when you will have objects of different types uniquely in the dictionary, usually user defined classes or structs, where the structs would need to contain some managed type references. But such methods wouldn't make so much sense for storing value by unique primitive types, so I would like to exclude such values from being mapped to the methods.
I understand there are other solutions (such as using KeyedByTypeCollection) to address the use case. Please don't answer the question by pointing to another solution to the use case. I'm just providing it as an example. I believe there must be many other use cases for have a managed constraint (probably as many as there are for the unmanaged case), and frankly I'm a little surprised that no one seems to have asked about this previously (at least I can't find anything about it).