-4

Hi,

I am getting "Structs cannot contain explicit parameterless constructor" error during build but i am able to compile the program with errors.

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

}

What are the possible ways to correct this error? Any help or guidance.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
MOZ
  • 768
  • 4
  • 19
  • 33

1 Answers1

1

Make the struct Program a class.

structs are immutable data-objects. There are plenty of recommendations when to use structs:

Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

Secondarily, the Program-constructor you have there would be better considered as main-method. Then call the constructor of your data-object with the three string-values as parameters.

Community
  • 1
  • 1
Patrik
  • 1,355
  • 12
  • 22