How to Resolve Error "CS0127: Since 'function' returns void, a return keyword must not be followed by an object expression" in C#
The Compiler Error CS0127 is a Return Type Mismatch error. The message reads: "Since 'MethodName' returns void, a return keyword must not be followed by an object expression".
In C#, the return type defined in a method's signature forms a strict contract. If a method is declared as void, it promises not to return any data to the caller. This error occurs when you attempt to break that promise by placing a value (like return 5;, return true;, or return "text";) inside a method marked as void.
This guide explains the relationship between method signatures and return statements and how to align them.
Understanding Void vs. Value Returns
voidMethod: Performs an action but sends nothing back. You can use thereturn;statement (with no value) to exit the method early, but you cannot usereturn value;.- Typed Method (e.g.,
int,string): Performs an action and must return a value of that specific type.
CS0127 happens when you mix these two concepts: declaring void but writing code as if it were Typed.
Scenario 1: Returning a Value from a Void Method
This usually happens when you copy-paste code from another method or misunderstand the purpose of the method.
Example of error:
public class Logger
{
// Signature: 'void' means "I return nothing"
public void LogMessage(string message)
{
System.Console.WriteLine(message);
// ⛔️ Error CS0127: Since 'LogMessage' returns void,
// a return keyword must not be followed by an object expression.
// You are trying to return 'true' (a boolean).
return true;
}
}
Scenario 2: Refactoring Mismatches
This often occurs during code cleanup. You might change a method signature from int to void because you realized you didn't need the result, but you forgot to update the return statements inside the method body.
Example of error:
public class Calculator
{
// Refactored from 'int Add' to 'void Add'
public void Add(int a, int b)
{
int result = a + b;
System.Console.WriteLine($"Result: {result}");
// ⛔️ Error CS0127: This line is left over from when the method returned 'int'.
return result;
}
}
Solution 1: Stop Returning Data
If the method is intended to be void (it just does work and finishes), simply remove the value from the return statement.
Early Exit (Valid)
You are allowed to use return to stop the function, provided you don't send data with it.
public class Logger
{
public void LogMessage(string message)
{
if (string.IsNullOrEmpty(message))
{
// ✅ Correct: 'return;' exits the void method without a value.
return;
}
System.Console.WriteLine(message);
}
}
End of Method (Valid)
If the return statement is at the very end of the method, you can simply delete it.
public class Calculator
{
public void Add(int a, int b)
{
int result = a + b;
System.Console.WriteLine($"Result: {result}");
// ✅ Correct: No return statement needed at the end of a void method.
}
}
Solution 2: Change the Return Type
If you actually need the value (e.g., to know if the operation succeeded, or to get the calculation result), you must update the method signature to match the data type you are returning.
public class Logger
{
// ✅ Correct: Changed 'void' to 'bool' so we can return success/failure.
public bool LogMessage(string message)
{
if (string.IsNullOrEmpty(message))
{
return false;
}
System.Console.WriteLine(message);
return true;
}
}
Async Methods: If you are working with asynchronous code:
async Taskis the equivalent ofvoid.async Task<T>is the equivalent of returning a typeT. If you see this error in anasync Taskmethod, it means you are trying toreturn value;. Change the signature toasync Task<Type>.
Conclusion
CS0127 enforces the contract of your method.
- Check the Signature: Is the return type
void? - Check the Body: Are you trying to
return 5;,return true;, orreturn obj;? - Decide:
- If you don't need the value, delete it (use
return;). - If you need the value, change the method signature from
voidto the correct type.
- If you don't need the value, delete it (use