Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created March 17, 2015 06:41
Show Gist options
  • Select an option

  • Save dnasca/9987e9eb7ea1ea7d9018 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/9987e9eb7ea1ea7d9018 to your computer and use it in GitHub Desktop.
Static and Instance class member
using System;
/* Important Static and Instance class member notes
*
* !@!@ Class members = fields, methods, properties, events, indexers, constructors, !@!@
*
* Static members are invoked using the class name
* Instance members are invoked using instances(objects) of that class
*
* An instance member belongs to a specific instance(object) of a class.
* If you have multiple objects of a class, each object will have have every instance member
*
* There will be only one copy of a static member, not matter how many instances of a class are created.
*/
class Circle
{
//pi will be made as a static field, and will therefor be a static member
static float _PI; /*
* mark as static because pi will remain static
* as pi remains static, we only need it to exist in 1 part of the memory
* and be shared by all of our future circle objects
*
*/
private int _Radius;
/*
* !@!@ static constructors are called BEFORE instance constructors are called, and even before the compiler refers to any static fields !@!@
*/
//example of a static constructor, name must be the same as the class. static constructors are used to init static fields in a class
static Circle()
{
Circle._PI = 3.1415f;
}
//create a constructor, same name as the Class
public Circle(int Radius) //radius is the parameter
{
this._Radius = Radius; //'this' radius parameter to initialize the above class field
} //we have used this constructor to initialize the Radius field
//create method to print the area of a circle
public static void Print() //<<-- *THIS IS A STATIC METHOD* >>--> ACCESS NOT FROM ANY FUTURE OBJECTS, BUT INSTEAD FROM THE CLASS, ex. Circle.Print()
{
//i'm just here for show
}
//create method to calculate area of a circle, and return it back
public float CalculateArea() //<<-- *THIS IS AN INSTANCE METHOD* >>--> You'll need to create an object '\.o_o./' to access this method
{
return Circle._PI * this._Radius * this._Radius; // 3.14*radius^2 // Remember, _PI is not accessed with 'this' because it is a static member, 'this' refers to an instance member
}
}
class Program
{
static void Main(string[] args)
{
/*****static members are invoked by the name of the class
*****instance members are invoked using instances(objects) of a class
*
* !@!@ There will only be one copy of a static member in memory, no matter how many objects we create. !@!@
*/
//create an object of the Circle Class, feed it an integer
Circle C1 = new Circle(5);
//using the instance of C1, born of the Circle class, we call to the **INSTANCE METHOD** of this(C1) object, and store it to Area
float Area = C1.CalculateArea();
Console.WriteLine("Area = {0}", Area);
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment