Creating instances of classes in C#
We already created instances of the simple Circle
class. We just needed to use the new
keyword followed by the class name, specify the required arguments enclosed in parentheses, and assign the result to a variable.
The following lines declare a public CalculateArea
method within the body of the Circle
class:
public double CalculateArea() { return Math.PI * Math.Pow(this.radius, 2); }
The method doesn't require arguments to be called. It returns the result of the multiplication of π by the square of the radius field value (this.radius
). The following lines show a new version of the Main
method. These lines create two instances of the Circle
class: circle1
and circle2
. The lines then display the results of calling ...
Get Learning Object-Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.