C# Language Keywords void

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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)
        return;

    // 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?