معنى Static: إني هقدر أنادي عليها من خلال الـ Class نفسه مش لازم أعمل Object منه عشان أقدر أنادي عليها ودا بيحصل لو هي مش معتمدة على Object معين أو بتختلف بإختلافهم
Instance vs Static
Instance method ⇒ Object Member method (Non static)
Instance Members
الـ Instance Members بتبقى مرتبطة بالـ object نفسه، وكل object يقدر ينادي على الـ instances الخاصة بيه.
var corn = new Person();// Calling an instance method on the objectcorn.Introduce();
امتا اعمل 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:
Call Static Method or Static Property
Create Object From This Class
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; }}