C# Static Method

Static illustration

Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this. Static methods can be public or private. They cannot use the this instance expression.

Static Modifier

Key point: Static methods are called without an instance reference.

Example

Note

This program defines both static methods and regular instance methods and calls them both. The static methods use the static keyword somewhere in the method declaration signature, usually as the first keyword or the second keyword after public.

Static methods cannot access non-static class level members and do not have a 'this' pointer. Instance methods can access those members, but must be called through an object instantiation, which causes another step and level of indirection.

Program that uses instance and static methods [C#]

using System;

class Program
{
    static void MethodA()
    {
	Console.WriteLine("Static method");
    }

    void MethodB()
    {
	Console.WriteLine("Instance method");
    }

    static char MethodC()
    {
	Console.WriteLine("Static method");
	return 'C';
    }

    char MethodD()
    {
	Console.WriteLine("Instance method");
	return 'D';
    }

    static void Main()
    {
	//
	// Call the two static methods on the Program type.
	//
	Program.MethodA();
	Console.WriteLine(Program.MethodC());
	//
	// Create a new Program instance and call the two instance methods.
	//
	Program programInstance = new Program();
	programInstance.MethodB();
	Console.WriteLine(programInstance.MethodD());
    }
}

Output

Static method
Static method
C
Instance method
Instance method
D
Main method

Overview. This program defines five methods, the first four being MethodA, MethodB, MethodC, and MethodD and the final one being the Main entry point. The execution engine first encounters and compiles the Main method. The MethodA static method is invoked and its internal logic is executed. Next the Program.MethodC parameterless method is invoked and it internally prints a message. It also returns a value, which is placed on the evaluation stack and printed.

Instance method usages. The program next uses the two instance methods defined in the text, the MethodB and MethodD methods. Because these two methods are instance methods not decorated with the 'static' keyword, you must create a new object instance to call them through. They are part of the Program class definition, so we create a new instance of the Program class and can then use the dot notation to invoke them.

Class Examples

Public and private

Star (asterisk) character

Many static methods you develop will need to be declared in the public accessibility domain. Utility classes often contain public static methods, and this results in no performance loss and a method that is available to every caller. You can also use the internal keyword to describe a method that must only be called in the same assembly as the method implementation. Private static methods are also useful as internal logic repositories for the class state.

Public Method Private Method

Static local variables

Const keyword

Static locals are a kind of storage-permanent local variable in the C and C++ programming language. This concept does not exist in the C# programming language and when translating programs you must use a type-level static variable and reference it instead. The static keyword is not valid in the method body. The const keyword however can be used to create a method-local constant definition.

Static classes

Static classes are an addition to the language and basically are a way to specify that a type must not be created as an instance. So a static class and a regular class are the same in every way except the language prevents you from creating a new instance of the static class. This could possibly reduce programming errors but has no other effects on the runtime. However, static constructors do have an effect on the behavior of the type.

Static Constructor

Performance

Performance optimization

Static methods are normally faster to invoke on the call stack than instance methods. There are several reasons for this in the C# programming language. Instance methods actually use the 'this' instance pointer as the first parameter, so an instance method will always have that overhead. Instance methods are also implemented with the callvirt instruction in the intermediate language, which imposes a slight overhead.

callvirt Instruction

Note: Changing your methods to static methods is unlikely to help much on ambitious performance goals, but it can help a tiny bit and possibly lead to further reductions.

Static properties

Property (Icon copyright Microsoft)

Properties are essentially a special type of method and are actually implemented the same way in the metadata format. In the metadata, properties have the word "get_" or "set_" prefixed to their identifiers. You can use static properties in the same way as you can have static methods. Properties show the same performance levels of methods.

Property Examples

Access routines

Code Complete

There are many ways you can use access routines and global variables in programming. The book Code Complete by Steve McConnell outlines strategies you can use to employ global variables in programs without creating maintainability problems over time. Mainly, he emphasizes the usage of access routines. In the C# language, you can use static properties as access routines and this is detailed here.

Code Complete: Book Review Global Variable

Static field usage

Let's describe the usage of fields in static methods and fields in instance methods. Static methods have no way of accessing fields that are instance fields, as instance fields only exist on instances of the type. Instance methods can however access static fields with no problem. This means that using the static keyword restricts the available members of the type in a certain member. You can sometimes fix compilation bugs by removing the static keyword on a method.

Static Field

Summary

The C# programming language

Static methods in the C# language differ from instance methods in both the method declarations and in the method call sites. Static methods apply to a type, not an instance of the type they are defined on. Static methods affect the performance and meaning of the code, and are useful for utility methods or for certain abstractions of pure logic in your programs.

Method Tips
.NET