C# 结构不能包含显式无参数构造函数
嗨 我得到的结构在构建期间不能包含显式的无参数构造函数错误,但我能够编译有错误的程序C# 结构不能包含显式无参数构造函数,c#,C#,嗨 我得到的结构在构建期间不能包含显式的无参数构造函数错误,但我能够编译有错误的程序 struct Program { string Name; string Degree; string Dpthead; //Constructor public Program () { Console.WriteLine("Enter the Program's Name: "); Name = Console.ReadLine()
struct Program
{
string Name;
string Degree;
string Dpthead;
//Constructor
public Program ()
{
Console.WriteLine("Enter the Program's Name: ");
Name = Console.ReadLine();
Console.WriteLine("Enter the Program's Degree Name: ");
Degree = Console.ReadLine();
Console.WriteLine("Enter the Head of Program : ");
Dpthead = Console.ReadLine();
}
public void PrintUProgramDetails()
{
Console.WriteLine("Program Name: {0} from {1}. Enrolled in {2} degree(s)", Name, Degree, Dpthead);
}
}
纠正此错误的可能方法有哪些?任何帮助或指导。使struct程序成为一个类
结构是不可变的数据对象。有很多建议:
如果类型的实例很小且通常很短,或者通常嵌入到其他对象中,请考虑定义一个结构而不是类
除非类型具有以下所有特征,否则不要定义结构:
它在逻辑上表示单个值,类似于integer、double等基本类型。
它的实例大小小于16字节。
它是不变的。
它不必经常装箱。
第二,你在那里的程序构造器最好被认为是主方法。然后用三个字符串值作为参数调用数据对象的构造函数。C 6.0的结构将能够显式地使用无参数构造函数。有什么可能的方法来纠正此错误?要么使用类,要么不使用构造函数。您不应该为程序使用结构,结构是不可变的数据对象。@HimBromBeere:结构不是不可变的。一个好的设计使它们不变。