Home
Map
void MethodUnderstand the void keyword. Methods that are void return no values, and we cannot assign to them.
C#
This page was last reviewed on Jun 5, 2021.
Void. In C# void means that a method returns no value. Void is useful in many programs. But there are some compile-time errors that may be involved.
Action
Method notes. Void methods receive parameters like all methods. They use a simple "return" statement that is followed by a semicolon.
return
First example. The void keyword is 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.
Here We declare and use a static void method (Example1). The control flow jumps to this method when you invoke it, but there is no result.
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, omit the static modifier. Methods are by default considered instance.
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(); } }
Static void method Instance void method
IL. 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
Detail The void method must have no elements on the evaluation stack when the "ret" instruction is reached.
.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 }
Error, notes. An error occurs 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.
Info Because void primarily impacts the compile-time processing of a program, no errors will be caused by void specifically at runtime.
Detail The void type will instead force compile-time errors. These are useful—they help us improve programs.
class Program { static void VoidMethod() { } static void Main() { int result = VoidMethod(); } }
Error 1: Cannot implicitly convert type void to int.
Overload, error. A compile-time error can be raised when you try to overload methods based on only their return types. This cannot be done.
Important 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.
Action. This is a delegate type that returns void. It makes it easier to create delegate method objects. Action is a generic class. We specify its argument types as type parameters.
A summary. We declared and invoked void instance and static methods. The void keyword is common in C# source code. It means that the code returns no value.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 5, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.