using System;
//Static members are invoked using class name, where as instance members are invoked using instances(objects) of the class
//An instance member belongs to specific instance(object) of a class.
// If I create 3 objects of a class, I will have 3 sets of instance members in the memory,
// where as there will every be only one copy of a staic member, no matter how many instances of a class are created.
//Class members = fields, methods, properties, events, indexers, constructors.
class Circle
{
//float _PI = 3.141F; //making several objects when called the instance
//static float _PI = 3.141F; //making only one time when called the instance
static float _PI;
int _Radius;
//Statkc consturctor
// used to initialize static fields in a class
// called only once
//access modifiers are not allowed on static constructors
static Circle() //automatically called when making an instance
{
Circle._PI = 3.141F;
}
public Circle(int Radius)
{
this._Radius = Radius;
}
public static void Print()
{
//
}
public float CalculateArea()
{
//return this._PI * this._Radius * this._Radius;
return Circle._PI * this._Radius * this._Radius; //if using static valiable, using class name
}
}
class Program
{
static void Main(string[] args)
{
Circle circle1 = new Circle(5);
float Area1 = circle1.CalculateArea();
Console.WriteLine("Area = {0}", Area1);
//Circle.Print(); //as of static method.
Circle circle2 = new Circle(6); //another object
float Area2 = circle2.CalculateArea();
Console.WriteLine("Area = {0}", Area2);
}
}
=====================================
test for static constructor and instance constructor
using System;
class Circle
{
public static float _PI;
int _Radius;
static Circle() //automatically called when making an instance
{
Console.WriteLine("static constructor called");
Circle._PI = 3.141F;
}
public Circle(int Radius)
{
Console.WriteLine("static constructor called");
this._Radius = Radius;
}
public float CalculateArea()
{
return Circle._PI * this._Radius * this._Radius;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Circle._PI);
}
}
might be refering "Method" page