用 `new()` 约束类型参数是什么意思?
What does it mean to constrain a type parameter with `new()`?
这是来自流行的 EmguCV 包的 class 签名。这是 Image
class - 不是所有的都很重要:
/// <summary>
/// An Image is a wrapper to IplImage of OpenCV.
/// </summary>
/// <typeparam name="TColor">Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz, Ycc, Rgb or Rbga)</typeparam>
/// <typeparam name="TDepth">Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)</typeparam>
public partial class Image<TColor, TDepth>
: CvArray<TDepth>, IImage, IEquatable<Image<TColor, TDepth>>
where TColor : struct, IColor
where TDepth : new()
具体来说,请注意
...
where TDepth : new() // <-- either Byte, SByte, Single, double, UInt16, Int16 or Int32
new()
如何将类型参数TDepth
限制为.NET整数类型?
没有。根据 documentation.
,所有 new()
保证是具有约束的泛型类型(在本例中 TDepth
)必须提供无参数构造函数并且它不是抽象的
这是来自流行的 EmguCV 包的 class 签名。这是 Image
class - 不是所有的都很重要:
/// <summary>
/// An Image is a wrapper to IplImage of OpenCV.
/// </summary>
/// <typeparam name="TColor">Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz, Ycc, Rgb or Rbga)</typeparam>
/// <typeparam name="TDepth">Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)</typeparam>
public partial class Image<TColor, TDepth>
: CvArray<TDepth>, IImage, IEquatable<Image<TColor, TDepth>>
where TColor : struct, IColor
where TDepth : new()
具体来说,请注意
...
where TDepth : new() // <-- either Byte, SByte, Single, double, UInt16, Int16 or Int32
new()
如何将类型参数TDepth
限制为.NET整数类型?
没有。根据 documentation.
,所有new()
保证是具有约束的泛型类型(在本例中 TDepth
)必须提供无参数构造函数并且它不是抽象的