Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 8 Language Overviewprevious at this levelnext at this level 8.7 Classesprevious at this levelnext at this level 8.7.8 Instance constructorsAn instance constructor is a member that implements the actions required to initialize an instance of a class. The example
using System;  
class Point  
{  
   public double x, y;  
   public Point() {  
      this.x = 0;  
      this.y = 0;  
   }  
   public Point(double x, double y) {  
      this.x = x;  
      this.y = y;  
   }  
   public static double Distance(Point a, Point b) {  
      double xdiff = a.x - b.x;  
      double ydiff = a.y - b.y;  
      return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);  
   }  
   public override string ToString() {  
      return string.Format("({0}, {1})", x, y);  
   }  
}  
class Test  
{  
   static void Main() {  
      Point a = new Point();  
      Point b = new Point(3, 4);  
      double d = Point.Distance(a, b);  
      Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d);  
   }  
}  
shows a Point class that provides two public instance constructors, one of which takes no arguments, while the other takes two double arguments.
If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS