CSharp - Type Creation Struct Constructor


You cannot declare a parameterless constructor for the struct you created.

The parameterless constructor is added by compiler.

The parameterless constructor performs a bitwise-zeroing of its fields.

When you define a struct constructor, you must explicitly assign every field.

You can't have field initializers in struct.

Here is an example of declaring and calling struct constructors:


using System;

struct Point{
       public int x, y;
       public Point (int x, int y) { 
          this.x = x; 
          this.y = y; 
       }/*from w w  w  . j a v a2s  .  c  o m*/

class MainClass{
   public static void Main(string[] args){
        Point p1 = new Point ();       // p1.x and p1.y will be 0
        Point p2 = new Point (1, 1);   // p1.x and p1.y will be 1