结构不能包含显式无参数构造函数 [英] Structs cannot contain explicit parameterless constructors
本文介绍了结构不能包含显式无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
struct emp
{
public int empno;
public string ename;
public emp(int no,string nam)
{
empno = no;
ename = nam;
}
public emp(int e)
{
empno = e;
ename =mostafa;
}
public emp()//为什么在这个承包商中给我错误
{
empno = 0;
ename =mostafa;
}
}
我收到此错误:
Structs不能包含显式无参数构造函数
解决方案
这是来自MSDN - Structs(C#编程指南)
< pre lang =midl> struct 可能不会声明默认构造函数(不带参数的构造函数)或析构函数。
请参阅,MSDN有时会提供帮助:)
hi,
当你使用对象语言时,你使用的课程更好
public class \\ ncn
{
private int empno;
private string ename;
public \\ temp( int no, string nam)
{
empno = no;
ename = nam;
}
public \\ temp( int e)
{
empno = e;
ename = mostafa;
}
public \\ temp() // 为什么在这个承包商中给我错误
{
empno = 0 ;
ename = mostafa;
}
public int Mpno
{
get
{
return empno;
}
}
public string 名称
{
get
{
return ename;
}
}
jerem
嗨。
每个struct都有一个默认构造函数,用于将对象初始化为零。因此,您可以为结构创建的构造函数必须使用一个或多个参数。
public < span class =code-keyword> struct ClassY
{
public int field1;
public ClassY(){} // CS0568,不能没有param构造函数
// 请尝试以下代码:
// public ClassY(int i)
// {
// field1 = i;
// }
}
public class ClassX
{
public static void Main()
{
}
}
struct emp { public int empno; public string ename; public emp(int no, string nam) { empno = no; ename = nam; } public emp (int e) { empno = e; ename = "mostafa"; } public emp() // why give me error in this contractor { empno = 0; ename = "mostafa"; } }
I am getting this error:
"Structs cannot contain explicit parameterless constructors"
解决方案
This is from MSDN - Structs(C# Programming Guide)
A struct may not declare a default constructor (a constructor without parameters) or a destructor.
See, MSDN does help sometimes :)
hi,
when you do a object language you use a class it is better
public class emp { private int empno; private string ename; public emp(int no, string nam) { empno = no; ename = nam; } public emp (int e) { empno = e; ename = "mostafa"; } public emp() // why give me error in this contractor { empno = 0; ename = "mostafa"; } public int Mpno { get { return empno; } } public string Name { get { return ename; } }
jerem
Hi.
Each struct already has a default constructor that initializes the object to zero. Therefore, the constructors that you can create for a struct must take one or more parameters.
public struct ClassY { public int field1; public ClassY(){} // CS0568, cannot have no param constructor // Try following instead: // public ClassY(int i) // { // field1 = i; // } } public class ClassX { public static void Main() { } }
这篇关于结构不能包含显式无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文