使用`new()`约束类型参数是什么意思?

时间:2015-07-08 19:53:01

标签: c# generics emgucv

这是来自流行的EmguCV包的类签名。这是Image课程 - 并非所有课程都很重要:

/// <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整数类型?

1 个答案:

答案 0 :(得分:6)

它没有。所有new()保证带有约束的泛型类型(在这种情况下为TDepth)必须提供无参数构造函数,并且它不是抽象的,符合documentation