簡體   English   中英

用`new()`約束類型參數是什么意思?

[英]What does it mean to constrain a type parameter with `new()`?

這是流行的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整數類型?

沒有。 所有new()保證都是帶有約束的泛型類型(在這種情況下為TDepth )必須提供無參數的構造函數,並且根據文檔 ,它不是抽象的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM