【发布时间】:2026-02-04 05:20:07
【问题描述】:
我无法理解这部分代码,请帮忙。
当我这样做时
public class TestClass
{
static TestClass(int i)
{
}
TestClass()
: this(1) // Error
{
}
}
它给了我错误
'TestApp.TestClass' 不包含带 1 个参数的构造函数
但是当我这样做时,它不会显示任何错误。
public class TestClass
{
TestClass(int i)
{
}
static TestClass()
: this(1)
{
}
}
请有人解释一下这种行为?
【问题讨论】:
-
静态构造函数不能指定参数。
-
@DanielA.White 虽然这是真的,但它允许您调用实例构造函数仍然很奇怪。
-
你的第二个也不编译。
-
@YuriyFaktorovich 如果它允许它会很奇怪,但它不允许它,正如我所期望的那样。
标签: c#