Skip to main content

How to Resolve Error "CS0673: System.Void cannot be used from C# -- use typeof(void) to get the void type object" in C#

The Compiler Error CS0673 is a syntax restriction error. The message reads: "System.Void cannot be used from C# -- use typeof(void) to get the void type object".

In C#, void is a keyword used to specify that a method does not return a value. Under the hood, this maps to the .NET type System.Void. However, C# does not allow you to use System.Void directly in your code (as a variable type, a return type, or a class reference). The only valid use for the concept of the void type object is during Reflection (inspecting code metadata), and for that specific scenario, C# mandates a specific syntax: typeof(void).

This guide explains how to fix this error when working with Reflection or Generics.

Understanding the Restriction​

Use void (lowercase keyword) when defining methods. Use typeof(void) when inspecting types via Reflection. Never use System.Void explicitly.

The compiler enforces this because System.Void is a special structure in the CLR that cannot be instantiated. You cannot have a variable of type System.Void.

Scenario 1: Reflection and Type Comparison​

This is the most common use case. You are iterating over methods in a class using Reflection, and you want to check if a specific method returns void.

Example of error: attempting to compare the return type directly to the System.Void type identifier.

using System;
using System.Reflection;

public class MethodInspector
{
public void CheckMethods()
{
MethodInfo[] methods = typeof(string).GetMethods();

foreach (var m in methods)
{
// ⛔️ Error CS0673: System.Void cannot be used from C#.
// C# forbids accessing the System.Void type directly like this.
if (m.ReturnType == System.Void)
{
Console.WriteLine($"{m.Name} returns nothing.");
}
}
}
}

Solution: Use typeof(void)​

To get the System.Type object representing void, use the typeof operator with the lowercase keyword.

Solution:

using System;
using System.Reflection;

public class MethodInspector
{
public void CheckMethods()
{
MethodInfo[] methods = typeof(string).GetMethods();

foreach (var m in methods)
{
// ✅ Correct: typeof(void) returns the metadata object for System.Void.
if (m.ReturnType == typeof(void))
{
Console.WriteLine($"{m.Name} returns nothing.");
}
}
}
}

Scenario 2: Generics and System.Void​

Sometimes developers try to use System.Void as a generic type argument, often to represent a "Task that returns nothing" or a "Result object with no value."

C# does not allow void (or System.Void) as a generic argument.

Example of error​

using System.Threading.Tasks;

public class Worker
{
// ⛔️ Error CS0673 (and CS1061): You cannot use System.Void as a generic argument.
// Developers sometimes try this when they want a generic wrapper that handles void.
public Task<System.Void> DoWorkAsync()
{
return null;
}
}

Solution: Use Task or a Unit Type​

If you need a Task that returns nothing, simply use the non-generic Task.

using System.Threading.Tasks;

public class Worker
{
// ✅ Correct: Standard Task represents a void async operation.
public Task DoWorkAsync()
{
return Task.CompletedTask;
}
}
note

Functional Programming "Unit": If you are using a library like MediatR or writing generic code (Func<T>) that requires a return type even when logical data doesn't exist, you cannot use void. The standard pattern is to define a custom struct named Unit or use bool or object as a dummy placeholder.

Conclusion​

CS0673 is a syntax enforcement error.

  1. Definitions: Always use the keyword void (lowercase) for method signatures.
  2. Reflection: Always use typeof(void) to check if a type is void.
  3. Generics: You cannot pass void or System.Void to a generic list or task. Use Task (non-generic) or a dummy Unit struct.