When is a value type / reference type constraint useful in C#?

£可爱£侵袭症+ 提交于 2019-12-07 03:28:56

问题


I'm looking for simple examples that demonstrate when the value type / reference type constraints are useful.

... where T : struct  // when is this useful?
... where T : class   // and what about this?

I remember seeing some very nice examples in the past but I just can't find them.


回答1:


It allows you to use as operator on T if it is T:class.

It forbids you to compare T with null if T is T:struct.

Note that if you omit T:class then you can compare T to null even when T is a value type.

[Note: I needed to edit this post a few times before it got correct. At least I hope it is now correct.]




回答2:


The primary usefulness that I've found in it lies with Marshalling and pinning the object in memory.

For example, I do a lot of work with internal structures that can't be auto-converted, or are sent down the wire as a byte stream, so I wrote this helper:

public static T PinAndCast<T>(this Array o) where T : struct
{
    var handle = System.Runtime.InteropServices.GCHandle.Alloc(o, GCHandleType.Pinned);
    T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return result;
}



回答3:


"T : class" will force the generic type specified to be a class, not a value. For example, we can make up an ObjectList class that requires the generic type specified to be a class, not a value:

class ObjectList<T> where T : class {
  ...
}

class SomeObject { ... }

ObjectList<int> invalidList = new ObjectList<int>();  //compiler error

ObjectList<SomeObject> someObjectList = new ObjectList<SomeObject>(); //this works

This forces an invariant on your generic type T that otherwise might not be enforceable. "T : struct" would work the same way. Note that you can also use this construct to enforce not only that the type T is a class, but also that it matches an interface. The code sample I took this from also has

class MyList<T> where T : class, IEntity { ... }

which forces T to be a class AND also be an IEntity.



来源:https://stackoverflow.com/questions/4733748/when-is-a-value-type-reference-type-constraint-useful-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!