Structure Constructors - CSharp Custom Type

CSharp examples for Custom Type:struct

Introduction

Unlike classes, if you decide to declare a constructor, you must include declarations with parameters.

You cannot declare a constructor for a structure that has no parameters.

Demo Code

struct point
{
   public int x;
   public int y;
   public point(int x, int y)
   {// w w  w . j  a v  a  2  s  .c  o  m
      this.x = x;
      this.y = y;
   }
}
class MainClass
{
   public static void Main()
   {
      point point1 = new point();
      point point2 = new point(8, 8);
      point1.x = 1;
      point1.y = 4;
      System.Console.WriteLine("Point 1: ({0},{1})", point1.x, point1.y);
      System.Console.WriteLine("Point 2: ({0},{1})", point2.x, point2.y);
   }
}

Result


Related Tutorials