C# Language Keywords void

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


The reserved word "void" is an alias of System.Void type, and has two uses:

  1. Declare a method that doesn't have a return value:
public void DoSomething()
    // Do some work, don't return any value to the caller.

A method with a return type of void can still have the return keyword in its body. This is useful when you want to exit the method's execution and return the flow to the caller:

public void DoSomething()
    // Do some work...

    if (condition)

    // Do some more work if the condition evaluated to false.
  1. Declare a pointer to an unknown type in an unsafe context.

In an unsafe context, a type may be a pointer type, a value type, or a reference type. A pointer type declaration is usually type* identifier, where the type is a known type - i.e int* myInt, but can also be void* identifier, where the type is unknown.

Note that declaring a void pointer type is discouraged by Microsoft.

Got any C# Language Question?