English 中文(简体)
为什么最后必须规定新的()限制?
原标题:Why must new() constraint be specified last?

C# compiler requires new() constraint to be specified last. According to MSDN:

When used together with other constraints, the new() constraint must be specified last.

为什么有这样的限制?

最佳回答

因为具体措词就是这样。 可能这样说,因为它使制约更加容易。 允许你具体说明任何命令中的制约因素没有什么价值,我可以想象,这样做会带来一些代价(包括机会成本!)。

事实上,我们注意到,具体描述只是说,如果你有的话,你必须把建筑限制放在最后。 这实际上说,你必须在以下顺序上受到制约:

  1. Primary constraint.
  2. Secondary constraints.
  3. Constructor constraint.

如果主要制约因素是具体规定了哪类参数必须是参考类型或价值类型,则次要制约因素是规定了基类或接口的制约因素,而施工限制是此处讨论的制约因素。

具体条款的相关部分是第10.1.5节,当然值得阅读。

问题回答

由于其他制约因素(例如阶级),你可以具体说明一名施工人,而其他制约因素则可能牵线搭桥。

因此,如果新(新)首先出现,那么对接口的制约因素,你就会出现错误,因为你可能对接口有新的()限制。

YAQAPD: 可是另一个关于养育指示的问题

Every programming language has it s own rules. The direction of parsing is one of them. Let me explain in more detail (be patient).

证明你在Pascal/Delphi写道:

function Sum2Numbers (n1, n2:integer) : integer;
begin
   result:=n1+n2;
end;

Now, the same function in C:

int function Sum2Numbers (int n1, int n2)
{
  return (n1+n2);
}

For the programmer s point of view, both function do the same thing (and really they do). However, behind the scenes, each compiler works on it s own way.

Pascal/Delphi and many other languages compile the code by parsing the text from LEFT TO RIGHT. So, when we call the function in Pascal or Delphi, the compiler put in the stack, first n1 and second n2. The same function in C does it from RIGHT TO LEFT, i.e. the compilers put in the stack, first n2 and second n1.

两位汇编者都阅读了他们所知道的笔记中的参数,因此所有工作都是罚款的。

所有“家庭”语言(C、C++、C++、C++/CLI、C#、......)都使用“平等法”指令。

This is the reason why the new() constraint MUST be specified in the FAR RIGHT of the constraint list.
The compiler must know IN ADVANCE that will need to create instances of classes, BEFORE use them.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签