Static
Static word can be used for class, methods, fields, etc. Using static usually boots performance and also allows faster invocation on the call. However you cannot use static to declare local variable in C#. You can use static with readonly field to initialize during runtime
public static readonly;
Static Class
- All members of the static class needs to be static.
- Constructor instance is eliminated in static class.
- Cannot be instantiated
- Is sealed, therefore it cannot be inherited
- They cannot inherit from any class except Object
- Can contain static constructor
static class Test { public static int _val = 5; }
Accessing members of static class
Can access its members by class name itself. Example: TestClass has method name MethodA
TestClass.MethodA();
Usage
Static class is mostly used to handle global variables and single-instance
Static class is mostly used when you want to operate on input parameter and does not have to get or set any internal instance fields such as System.Math. Static class remains in memory for the lifetime of the application domain.
Static Methods
Constant or type declaration is implicitly a static member. It is referenced through the type name and not through instance.
public class myBase { public struct MyStruct { public static int x = 100; } }
To access the static member:
Console.WriteLine(MyBaseC.MyStruct.x);
Notes:
- You cannot use ‘this’ to reference static methods
- Static methods can be public or private
- Private static methods: by default it is private, important for internal logic repositories for class state
- Public static methods: available through out the program, static members cannot access non-static class level members
- Static methods cannot be overwritten or overloaded
- Static methods cannot be defined through interfaces
Utility methods perform common, often re-used functions which are helpful for accomplishing routine programming tasks.
Static Fields
Shared among all instances of the class, it belongs to the type not to the object. You will need to assign value explicitly to the static field. Static fields with static class we can call directly without initiation.
class Test { static int x = y; static int y = 5; static void Main() { Console.WriteLine(Test.x); Console.WriteLine(Test.y); Test.x = 99; Console.WriteLine(Test.x); } }
Static Properties
- Similar to static methods
- Have the word “get_” or “set_” prefixed to their identifiers
- Static properties are a good function type to put inside a static class.
- Static properties can be accessed with the composite name, using the dot notation.
namespace Rextester { public class Program { public static void Main(string[] args) { Console.WriteLine(DayManagement.DayInNumber); Console.WriteLine(DayManagement.NameOfDay); } } static class DayManagement { public static int DayInNumber { get { return DateTime.Today.Day;} } public static string NameOfDay { get { return DateTime.Today.DayOfWeek.ToString();} } } }
Notes
Static properties belong to a class, rather than to an instance of a class and they are shared across entire project. Basically it is just one copy per run time environment. It is something that will not change from instance to instance. It acts like a globally accessible function and available from anywhere in the program. It is important to have if you have a function which needs to be reused.
Static Constructor
- A static constructor executes once per type instead of once per instance.
- A type/class can define only one static constructor, and it must be parameter-less and same name as the type/class and no access modifiers.
- The modifiers allowed by static constructors are unsafe and extern.
- Static constructor initializers fields before the static constructor is called.
- Runs at indeterminate time before those fields or static members are used and creates overhead.
- Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.
- A static constructor cannot be called directly.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
class Test { static Test() { Console.WriteLine ("Type Initialized"); } }
Static Members
You can also have static members such as:
- Static Array
- Static Dictionary
- Static Regex
- Static String