-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.

4
  • C# 6.0's structure will be able explicit parameterless constructor Commented Jul 7, 2015 at 9:57
  • 2
    "What are the possible ways to correct this error?" Either use a class or don't use the constructor. Commented Jul 7, 2015 at 10:03
  • You should not use a struct for a program, Structs are immutable data-objects. Commented Jul 7, 2015 at 10:09
  • 1
    @HimBromBeere: Structs are not immuatable. A good design makes them immutable. Commented Jul 7, 2015 at 10:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.