CSharp - Type Creation Struct Constructor

Introduction

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:

Demo

using System;

struct Point{
       public int x, y;
       public Point (int x, int y) { 
          this.x = x; 
          this.y = y; 
       }//from  w ww  .  j av a 2 s .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
        
        Console.WriteLine(p1.x);
        Console.WriteLine(p2.x);
   }
}

Result

Quiz