9866550012
CSHARP CLASSES AND CONSTRUCTORS
Example 1
The following example demonstrates a class with two class constructors, one without arguments and one with two arguments.
// Constructor1.cs
using System;
class Point
{
public int x, y;
// Default constructor:
public Point()
{
x = 0;
y = 0;
}
// A constructor with two arguments:
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// Override the ToString method:
public override string ToString()
{
return(String.Format("({0},{1})", x, y));
}
}
class MainClass
{
static void Main()
{
Point p1 = new Point();
Point p2 = new Point(5,3);
// Display the results using the overriden ToString method:
Console.WriteLine("Point #1 at {0}", p1);
Console.WriteLine("Point #2 at {0}", p2);
}
}
Output
Point #1 at (0,0) Point #2 at (5,3)
Example 2
In this example the class
Person does not have any constructors, in which case, a default constructor is automatically provided and the fields are initialized to their default values.// Constructor2.cs
using System;
public class Person
{
public int age;
public string name;
}
public class MainClass
{
static void Main()
{
Person p = new Person();
Console.Write("Name: {0}, Age: {1}",p.name, p.age);
}
}
Output
Name: , Age: 0
Notice that the default value of
age is 0 and the default value of name is null. For more information on default values, see Default Values Table.Example 3
The following example demonstrates using the base class initializer. The
Circle class is derived from the general class Shape, and the Cylinder class is derived from the Circle class. The constructor on each derived class is using its base class initializer.// CtorInitializer.cs
using System;
abstract class Shape
{
public const double pi = Math.PI;
protected double x, y, z;
public Shape (double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public abstract double Area();
}
class Circle: Shape
{
public Circle(double radius): base(radius,0,0)
{
}
public override double Area()
{
return pi*x*x;
}
}
class Cylinder: Circle
{
public Cylinder(double radius, double height): base(radius)
{
y = height;
}
public override double Area()
{
return 2*(base.Area()) + 2*pi*x*y;
}
}
class TestClass
{
public static void Main()
{
double radius = 2.5, height = 3.0;
Circle myCircle = new Circle(radius);
Cylinder myCylinder = new Cylinder(radius, height);
Console.WriteLine("Area of the circle = {0:F2}",
myCircle.Area());
Console.WriteLine("Area of the cylinder = {0:F2}",
myCylinder.Area());
}
}
Output
Area of the circle = 19.63 Area of the cylinder = 86.39
Example
The following is an example of a class using a private constructor.
// PrivateCtor1.cs
using System;
public class MyClass
{
private MyClass() {}
public static int counter;
public static int IncrementCounter()
{
return ++counter;
}
}
class MainClass
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// MyClass myObject = new MyClass(); // Error
MyClass.counter = 100;
MyClass.IncrementCounter();
Console.WriteLine("New count: {0}", MyClass.counter);
}
}
Output
New count: 101
Notice that if you uncomment the following statement from the example, it will generate an error because the constructor is inaccessible due to its protection level:
// MyClass myObject = new MyClass(); // error
Example
In this example, the class
MyClass has a static constructor and one static member, MyMethod(). When MyMethod() is called, the static constructor is invoked to initialize the class.// StaticCtor1.cs
using System;
class MyClass
{
// Static constructor:
static MyClass()
{
Console.WriteLine("The static constructor invoked.");
}
public static void MyMethod()
{
Console.WriteLine("MyMethod invoked.");
}
}
class MainClass
{
static void Main()
{
MyClass.MyMethod();
}
}
Output
The static constructor invoked. MyMethod invoked.
No comments:
Post a Comment