C# Void

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

Example

Return keyword

First, we note that 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.

Keyword: keywords direct program behavior

Parameters:Void methods receive parameters like all methods—they use a simple "return" statement that is followed by a semicolon.

Return

Here:The example demonstrates the void keyword in instance and static methods.

Static Method
C# program that declares void type methods

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
Method call

In this example, we declare and use a static void method. The Example1 method is a parameterless void method. The control flow jumps to this method when you invoke it, but there is no result and you cannot influence it with arguments.

Note:It is a compile-time error to assign to the Example method invocation. No variable can be assigned to void.

Next, we use an instance void method. To declare an instance method, we omit the static modifier. Methods are by default considered instance. We see a statement that results in a compile-time error, caused by assigning to a void method.

Compile-time error

Warning: exclamation mark

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. The void type will instead force compile-time errors.

Compile-Time Error

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. A compile-time error can be 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.

Overload

Note:The IL supports overloading on return type. It identifies methods based on a triad: return type, identifier, and parameter list.

IL

Framework: NET

Next, we look at the intermediate language generated by the Microsoft C# compiler for the first example method. The intermediate language (IL) relies on an evaluation stack for processing.

Intermediate Language

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.

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
}

Action

The Action type is a delegate type that returns void. It makes it easier to create delegate method objects that return no value. Action is a generic class. You specify its argument types as type parameters.

Action

Summary

The C# programming language

We looked at the void keyword and return type. We declared, and invoked, void instance and static methods. The void keyword is extremely common in C# source code and demands that the code not return a value.

Review:We saw how the void return type is implemented in the high-level assembly language.

And:We looked at some compile-time errors related to the void keyword on instance and static methods.


C#: Method