Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"
Posted
Updated 3-Nov-10 20:12pm
v2

This is from MSDN - Structs(C# Programming Guide)

MIDL
A struct may not declare a default constructor (a constructor without parameters) or a destructor.


See, MSDN does help sometimes :)
 
Share this answer
 
Comments
Dalek Dave 4-Nov-10 5:02am    
Only sometimes! :)
Toli Cuturicu 6-Nov-10 15:30pm    
When??
hi,

when you do a object language you use a class it is better

C#
 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
 
Share this answer
 
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.
C#
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()
   {
   }
}
 
Share this answer
 
Comments
Moxxis 7-Apr-15 16:14pm    
The "Therefore" is not appropriate (in MSDN's "explanation".) It does not follow that you should not be able to replace the default behavior of a struct constructor. Initializing the size field for Windows API structs is an outstanding example. MS may have a reason for disallowing this, but they have not stated it in the help.
sustituye el constructor sin parametros public, por static.

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";
}

static emp() // why give me error in this contractor
{
empno = 0;
ename = "mostafa";
}
}
 
Share this answer
 
Comments
Richard Deeming 26-Nov-15 13:18pm    
This question was asked, and solved, over FIVE YEARS AGO. Your answer adds nothing to the previous answers.

Also, this is an English speaking site. With the exception of the two specific "Non English Language" forums, all posts need to be in English.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900