C# Void Type

Void keyword

A void is empty. The void keyword in the C# language indicates that a method returns nothing. When a void method is invoked, it has no result and no variable can be assigned. Void is useful throughout your source code.

Keywords

This C# example program shows the void keyword. Void methods return no values.

Example

Note

The void keyword is extremely common and useful. It indicates the evaluation stack of a method must be empty when it returns and no value will be copied into the calling code. Void methods receive parameters like all methods—they use a simple "return" statement that is followed by a semicolon. The example demonstrates the void keyword in instance and static methods.

Program that declares void type methods [C#]

using System;

class Program
{
    static void Example1()
    {
	Console.WriteLine("Static void method");
    }
    void Example2()
    {
	Console.WriteLine("Instance void method");
	return; // Optional
    }

    static void Main()
    {
	//
	// Invoke a static void method.
	//
	Example1();
	//
	// Invoke an instance void method.
	//
	Program program = new Program();
	program.Example2();
	//
	// This statement doesn't compile.
	//
	// int x = Example1();
    }
}

Output

Static void method
Instance void method

Static void method. The program first shows how you can declare and use a static void method. The Example1 method is a parameterless void method, which means that the control flow jumps to this method when you invoke it, but there is no result of the method and you cannot influence its behavior with arguments. It is a compile-time error to assign to the Example method invocation.

Instance void method. The program also demonstrates the usage of an instance void method. To declare an instance method, you must omit the static modifier; methods are by default always considered instance in the C# language. The program finally indicates a statement that would result in a fatal compile-time error, caused by assigning to a void method.

Compile-time errors

Warning

Because the void type primarily impacts the compile-time processing of a program and not the runtime of the program, no errors will be caused by void specifically at runtime. Instead, the void type will force compile-time errors because the source text is incorrect.

One error you may get is when you try to assign a variable to the result of a void method. This is the "cannot implicitly convert type" error. To fix this, do not assign to void method expressions.

Error 1 Cannot implicitly convert type 'void' to 'int'

Overloading by return type. Another program that can be caused by using void types incorrectly is a compile-time error raised when you try to overload methods based on their return types. You cannot overload methods based on their return types such as void alone in the C# language.

Note: The intermediate language actually supports overloading on return type, because it identifies methods based on a triad of their return type, identifier, and parameter list.

Intermediate language

.NET Framework information

Let's look at the intermediate language generated by the Microsoft C# compiler for the first example method in this article. The intermediate language relies on an evaluation stack for processing. The void method must have no elements on the evaluation stack when the "ret" instruction is reached. The book Expert .NET IL 2.0 Assembler by Serge Lidin provides an excellent description of this requirement.

Expert .NET 2.0 IL Assembler Review
Intermediate language for void method [IL]

.method private hidebysig static void Example1() cil managed
{
    .maxstack 8
    L_0000: ldstr "Static void method"
    L_0005: call void [mscorlib]System.Console::WriteLine(string)
    L_000a: ret
}

Summary

The C# programming language

We looked at the void keyword and return type in the C# language and saw how you can declare and invoke void instance and static methods. The void keyword is extremely common in C# source code and demands that the code not return a value. We saw how the void return type is implemented in the high-level assembly language and also looked at some compile-time errors related to the void keyword on instance and static methods.

Method Tips
.NET