Static Method

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()


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()
        Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));

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

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