构造函数中的参数:结构与类
本文关键字:结构 参数 构造函数 | 更新日期: 2023-09-27 18:14:57
为什么下面的代码片段不能工作?
public struct AStruct
{
public bool Fi { get; set; }
public string Fei{ get; set; }
public bool Fo{ get; set; }
public string Fam{ get; set; }
public AStruct(bool fi, string fei, bool fo, string fam)
: this()
{
this.Fi = fi;
this.Fei = fei;
this.Fo = fo;
this.Fam = fam;
}
}
,最后在控件
的构造函数中调用public GS(AStruct astruct)
{}
Visual Studio 2012和编译器报错,没有找到AStruct。
如果改成
public class AStruct
{
....
}
public AStruct(bool fi, string fei, bool fo, string fam)
{
....
}
它不再抱怨了…
什么线索吗?
因为AStruct
没有无参数构造函数,编译器会报错。那么为什么要麻烦地将: this()
添加到参数构造函数中呢?
然后,结构不能包含无参数的构造函数[MSDN]。
我读了一点,我现在猜你有一个名称空间的问题。如果名称空间是正确的,我建议您尝试重新启动Visual Studio并进行重建。我有时会遇到无法解释的错误,在重新启动Visual Studio并重新构建之后,问题就解决了。
否则,我们能得到错误代码吗?CS…