• ู…ุนู†ู‰ Static: ุฅู†ูŠ ู‡ู‚ุฏุฑ ุฃู†ุงุฏูŠ ุนู„ูŠู‡ุง ู…ู† ุฎู„ุงู„ ุงู„ู€ Class ู†ูุณู‡ ู…ุด ู„ุงุฒู… ุฃุนู…ู„ Object ู…ู†ู‡ ุนุดุงู† ุฃู‚ุฏุฑ ุฃู†ุงุฏูŠ ุนู„ูŠู‡ุง ูˆุฏุง ุจูŠุญุตู„ ู„ูˆ ู‡ูŠ ู…ุด ู…ุนุชู…ุฏุฉ ุนู„ู‰ Object ู…ุนูŠู† ุฃูˆ ุจุชุฎุชู„ู ุจุฅุฎุชู„ุงูู‡ู…

Instance vs Static

Instance method โ‡’ Object Member method (Non static)

Instance members

  • ุจูŠุจู‚ุง ู„ูŠู‡ุง ุนู„ุงู‚ุฉ ุจุงู„ object ู†ูุณู‡ ูˆูƒู„ object ูŠู‚ุฏุฑ ูŠู†ุงุฏูŠ ุนู„ู‰ ุงู„ instances ุงู„ุฎุงุตุฉ ุจู‡
var corn = new Person();
corn.Introduce(); // Instance

Link to original

Static Members

  • ุชู‚ุฏุฑ ุชูˆุตู„ู‡ุง ู…ู† ุฎู„ุงู„ ุงู„ class ู†ูุณู‡
  • ุจู†ุณุชุฎุฏู…ู‡ุง ููŠ ุงู„ุญุงุฌุงุช ุงู„ู„ูŠ ู…ุด ู…ู†ุทู‚ูŠ ุชุจู‚ุง ู…ูˆุฌูˆุฏุฉ ููŠ ุฃูƒุชุฑ ู…ู† ู…ูƒุงู† ุฒูŠ ู…ุซู„ู‹ุง ุงู„ WriteLine
    • ุงู†ูƒ ุชุนุฑู ูˆู‚ุช ุฏู„ูˆู‚ุชูŠ ู…ุด ุงู„ู…ูุฑูˆุถ ู„ูƒู„ object ุจุณ ู‡ูˆ ู„ูƒู„ุงุณ ุจุณ DateTime.Now
    • ุนู†ุฏูŠ console ูˆุงุญุฏ ูˆู‡ูˆ ุฏุง ุงู„ู„ูŠ ู…ู…ูƒู† ุฃุทุจุน ู…ู† ุฎู„ุงู„ู‡ ูˆู‡ูˆ ูƒู„ุงุณ Console.WriteLine()
public class Person 
{
	public static int PeopleCount = 0; // Static
}
Link to original

  • ูƒู…ุงู† ุงุณุชุฎุฏู…ู†ุง ุงู„ู€ Static ููŠ ุงู„ู€ Cs Operator Overloading ูˆูƒู…ุงู† ุงู„ู€ Cs Casting Operator

Why static class

ุงู…ุชุง ุงุนู…ู„ method static ู„ู…ุง ูŠูƒูˆู† ู†ุงุชุฌ ุงู„ู€ method ู…ุด ุจูŠุฎุชู„ู ุจุฅุฎุชุงู„ู ุงู„ู€object

  • Static Class: is a just Container For Static Members (Attribute, Property, Constructor, Method) and Constants
  • You Canโ€™t Create Object From This Class (Helper or utility Class)
  • No Inheritance for this Class ู…ุด ู‡ูŠู†ูุน ู„ุฃู†ู‡ ู…ุณุงุนุฏ ู„ูŠุง ู…ุด ุฃูƒุชุฑ
  • Static Constructor (Maximum Only One Static Constructor Per Class)
  • Canโ€™t Specify Access Modifiers or Parameters for Static Constructor
  • Will be Executed Just Only One Time Per Class Lifetime Before the first Usage of Class

Usages Of Class:

  1. Call Static Method or Static Property
  2. Create Object From This Class
  3. Create Object From Another Class Inheriting From This Class

Example

  • ู‡ู†ุนู…ู„ Utility class: ุจูŠุจู‚ุง ููŠู‡ุง ุญุงุฌุงุช ู…ุซู„ู‹ุง ู‡ุชุณุงุนุฏู†ูŠ ููŠ ุงู„ุดุบู„
class Utility
{
    public int X { get; set; }
    public int Y { get; set; }
 
    public Utility(int _X , int _Y)
    {
        X = _X;
        Y = _Y;
        //pi = 3.14; 
        ///Object Ctor is not a right place for Initializing Static Members
        Console.WriteLine("Ctor");
    }
 
    ///Static Ctor
    ///will be executed only Once per class lifetime (Program)
    ///can't specify return Datatype , Access modifier or Input -- 
    //Paramters for static Ctor
    ///ctor : static Ctor in il
    ///Max only one static ctor per class
    ///We use it to initialize static attributes
    static Utility()
    {
        Console.WriteLine("Static Ctor");
        pi = 3.14;
    }
 
    public static double Cm2Inch (double Cm)
    {
        return Cm / 2.54;
    }
 
    public static double Inch2Cm (double Inch)
    {
        return Inch * 2.54;
    }
 
    static double pi;
    ///Static Attributes Allocate in Heap before First usage 
    //to Class till end of program (or unreachable in case of reference Types)
    ///Static members initialized to default values by Default
 
 
    ///Class Member Property : Static Property
    ///Static Property GET and SET One of These:
    ///1. Static Attribute
    ///2. Constant
    public static double PI { get { return pi; } }
 
	private const double pi = 3.14;
 
    public static double CircleArea(double R)
    {
        return pi * R * R;
    }
}