What exactly is a “Special Class”?

后端 未结 7 1915
鱼传尺愫
鱼传尺愫 2020-12-23 13:21

After failing to get something like the following to compile:

public class Gen where T : System.Array
{
}

with the error

7条回答
  •  有刺的猬
    2020-12-23 13:25

    I found a Jon Skeet comment from 2008 on a similar question: Why is the System.Enum constraint not supported.

    I know this is a bit off topic, but he asked Eric Lippert (the C# team) about it and they provided this answer:

    First off, your conjecture is correct; the restrictions on constraints are by and large artefacts of the language, not so much the CLR. (Were we to do these features there would be a few minor things we'd like to change in the CLR regarding how enumerable types are specified, but mostly this would be language work.)

    Second, I would personally love to have delegate constraints, enum constraints, and the ability to specify constraints that are illegal today because the compiler is trying to save you from yourself. (That is, making sealed types legal as constraints, and so on.)

    However, due to scheduling restrictions, we will likely not be able to get these features into the next version of the language.

提交回复
热议问题