Chapter 14
Static
The static keyword may be accustomed declare fields ANd ways which will be accessed while not having to form an instance of the category. Static (class) members solely exist in one copy that belongs to the category itself, whereas instance (non-static) member’s square measure created as new copies for every new object. This suggests that static ways cannot use instance members since these ways aren't a part of AN instance. On the opposite hand, instance ways will use each static and instance members.
class MyCircle
Three.14 F;
// Instance methodology float GetArea()
// Static/class methodology
static float ComputeArea(float a)
}
Accessing static members
To access a static member from outside the category, the category name is employed followed by the dot operator. This operator is that the same because the one accustomed access instance members, however to succeed in them AN object reference is needed. AN object reference can't be accustomed access a static member.
Static void Main ()
Static ways
The advantage of static members is that they will be employed by different categories while not having to form AN instance of the category. Fields ought to so be declared static once solely
a single instance of the variable is required. Ways ought to be declared static if they perform a generic operate that's freelance of any instance variables. a decent example of this is often the System.Math class, that provides a mess of mathematical ways.
This category contains solely static members and constants.
Static void Main ()
Static fields
Static fields have the advantage that they persist throughout the lifetime of the appliance.
A static variable will so be used, for instance, to record the quantity of times that a technique has been referred to as.
static int count = 0; public static void Dummy()
The default price for a static field can solely be set once before its 1st used.
Static categories
A class also can be marked static if it solely contains static members and constant fields. A static category can't be hereditary or instantiated into AN object. Trying to try and do thus can cause a compile-time error.
static category MyCircle
Static creator
A static creator will perform any actions required to initialize a category. Typically, these actions involve initializing static fields that can't be initialized as they're declared. This could be necessary if their formatting needs quite one line, or another logic, to be initialized.
class MyClass
part : array) part = i++;
}
}
The static creator, in distinction to the regular instance creator, can solely be run once. This happens mechanically either once AN instance of the category is made, or once a static member of the category is documented. Static constructors can't be referred to as directly and aren't hereditary. Just in case the static fields even have initializers, those initial values are going to be allotted before the static creator is run.
Extension ways
A new feature in C# three.0 is extension ways, which give the way to on the face of it add new instance ways to AN existing category outside its definition. AN extension methodology should be outlined as static during a static category and therefore the keyword this is often used on the primary parameter to designate that category to increase.
static category MyExtensions
{
// Extension methodology
public static int ToInt(this string s) come back Int32.Parse(s);
}
}
The extension methodology is owed for objects of its 1st parameter kind, in this
Case string, as if it had been AN instance methodology of that category. No relation to the static category is required.
class MyApp
">
}
Because the extension methodology has AN object reference, it will create use of instance members of the category it's extending. However, it cannot use members of that category that square measure inaccessible because of their access level. The good thing about extension ways is that they permit you to “add” ways to a category while not having to change or derive the initial kind.