Static

for Classes, Fields, Methods, Properties


Static Classes

A good example is System.Math


Cannot implement an interface and cannot be derived from
Cannot be instantiated from using the new keyword (as they are sealed)
All members must be declared static
Cannot contain any instance members
Remains in memory for the lifetime of the app domain in which your code is running
Is the same as a non-static class that contains only static members
Any constructors must also be declared static



Static Fields

They are also known as class static fields, static class variables or static member data.
These are useful when you want to define a value that applies to all objects of a given type.
Often used to keep a count of the number of objects that have been instantiated.
Often used to store a value that must be shared amoing all the instances.

public class Animal 
{
   private int _Age;
   private static int _Count;
}
class Program
{
   static void Main(string[] args)
   {
      Animal mydog = new Animal();
      Animal mycat = new Animal();
      mydog.Age = 2;
      Animal._Count += 1;
      System.Console.WriteLine(Animal.Total);

      mycat.Name = "two";
      Animal._Count += 1;
      System.Console.WriteLine(Animal.Total);

      System.Console.ReadLine();
   }
}

Static Constructor

A non static class can contain a static constructor
A static constructor allows you to control the initialisation of static variables.
A static constructor will be called before a class is used.



Static Constants

You cannot declare a constant as static because it is static by definition.
Constant fields are accessed the same way as static fields. ClassName.myconstant



Static Read Only Constants

A static read-only field must be initialized in a static constructor.



Static Methods

A non static class can contain static methods, properties or events
Cannot access non-static fields and events
Can be overloaded
Cannot be overridden


Applies to fields, methods, properties, events, operators


Can be called without creating an instance of the class
Also referred to as class methods or static members
C# does not support static local variables (VBA does support static local variables)


All methods are instance members unless explicitely marked with the keyword static
The vast majority of class methods will be instance members that are accessed from an object reference
These are associated with the class and are only allocated once

public MyClass 
{
   private static void method()
   {}
}

MyClass.method 


This allows you to use methods of the class without instantiating any objects
This is frequently used if you have a method that will always perform the same action


public class Rectangle 
{
    private int width, height;

    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public void OutputArea()
    {
        System.Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));
    }

    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}

Static Events




© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext