Member-only story
C# — Static Class, Methods, Constructors, Fields
Static is a keyward that is used with fields, methods, properties and classes when we make these members static they are globally accessble in all over the application.
Static Fields:
Static fields are those fields that are accessible all over the application. When you want to access a field in all over the application with out creating instances of class we make it static.
Example
public class MyClass
{
public static int StaticField = 42;
}
// Access the static field
int value = MyClass.StaticField;
Static Methods:
A Static method is also accessible all over the application globally. When we want that a method will be accessible in all over the application with creating the instance of class then we make it static.
Example
public class MathUtils
{
public static int Add(int a, int b)
{
return a + b;
}
}
// Call the static method
int result = MathUtils.Add(5, 3);
Static Properties:
Static properties provide access to data that is associated with the class itself globally. They are used similarly to static fields but can have custom getter and setter logic.