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

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

最佳答案

事实并非如此。根据 documentation,所有 new() 保证是具有约束的泛型类型(在本例中为 TDepth)必须提供无参数构造函数并且它不是抽象的.

关于c# - 用 `new()` 约束类型参数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302408/

相关文章:

c# - 使用 Json.Net 转换为 XML 时如何序列化空数组

c# - 在可扩展工厂中使用泛型?

c# - 在 .NET 中使用后将对象设置为 Null/Nothing

c# - 文本字段的条件验证

ios - 我的模板/通用 Swift 初始化程序/构造函数有什么问题?

java - java中基于父类(super class)和子类对泛型类型的多重限制

image-processing - cv Remap 替换 cv WarpPerspective

c# - MODI.Image 来自(位图)图像未保存在硬盘上。

c# - EMGU CV 中未处理 AccessViolationException

c# - 这是什么事件?