13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 <strong>Language</strong> Overviewclass Test{static void Main() {Stack s = new Stack();s.Push(1);s.Push(2);s.Push(3);s[0] = 33; // Changes the top item from 3 to 33s[1] = 22; // Changes the middle item from 2 to 22s[2] = 11; // Changes the bottom item from 1 to 11}}shows an indexer for the Stack class.8.7.8 Instance constructorsAn instance constructor is a member that implements the actions required to initialize an instance of a class.The exampleusing 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, whilethe other takes two double arguments.If no instance constructor is supplied for a class, then an empty one with no parameters is automaticallyprovided.8.7.9 DestructorsA destructor is a member that implements the actions required to destruct an instance of a class. Destructorscannot have parameters, they cannot have accessibility modifiers, and they cannot be called explicitly. Thedestructor for an instance is called automatically during garbage collection.39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!