Return values let methods send back results to where they were called. Void methods do something but don't send anything back.
Return values and void methods in C Sharp (C#)
returnType MethodName(parameters) {
// code
return value; // only if returnType is not void
}
void MethodName(parameters) {
// code
// no return value
}The return keyword sends a value back from the method.
Methods with void do not return anything and do not use return with a value.
int Add(int a, int b) { return a + b; }
void PrintHello() { Console.WriteLine("Hello!"); }
string GetGreeting(string name) {
return $"Hello, {name}!";
}This program has a method Multiply that returns the product of two numbers. The ShowResult method prints the result but returns nothing. The Main method calls both to show how return values and void methods work together.
using System; class Program { static int Multiply(int x, int y) { return x * y; } static void ShowResult(int result) { Console.WriteLine($"The result is {result}"); } static void Main() { int product = Multiply(4, 5); ShowResult(product); } }
Use return values when you need to use the result later.
Void methods are good for actions like printing or changing data without needing to send anything back.
Once a method hits a return statement, it stops running.
Return values send data back from methods.
Void methods do not send data back.
Use return values to get results; use void for actions only.
Practice
void method in C#?Solution
Step 1: Understand the meaning of
The keywordvoidin C#voidmeans the method does not return any value.Step 2: Compare with other return types
Methods with other return types likeint,string, orboolmust return a value of that type, butvoidmethods do not return anything.Final Answer:
It does not return any value. -> Option AQuick Check:
Void means no return value [OK]
- Thinking void methods return a default value
- Confusing void with returning null
- Assuming void methods must return something
Solution
Step 1: Identify method return type and return statement
The method declaresintreturn type, so it must return an integer value usingreturn.Step 2: Check each option for correct syntax
public int GetNumber() { return 5; } correctly declaresintand returns 5. public void GetNumber() { return 5; } is void but tries to return a value (invalid). public int GetNumber() { Console.WriteLine(5); } returns nothing despiteintreturn type. public void GetNumber() { Console.WriteLine(5); return 5; } is void but tries to return a value (invalid).Final Answer:
public int GetNumber() { return 5; } -> Option DQuick Check:
Return type matches returned value [OK]
- Returning a value from a void method
- Not returning a value from a non-void method
- Using Console.WriteLine instead of return
public class Program {
public static int Add(int a, int b) {
return a + b;
}
public static void Main() {
int result = Add(3, 4);
Console.WriteLine(result);
}
}Solution
Step 1: Understand the Add method
The methodAddtakes two integers and returns their sum.Step 2: Trace the Main method execution
Main callsAdd(3, 4), which returns 7. Then it prints 7 to the console.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings
- Confusing return with print
- Expecting void method to return value
public int Multiply(int x, int y) {
int product = x * y;
}Solution
Step 1: Check method return type and body
The method declaresintreturn type but does not return any value.Step 2: Identify missing return statement
Since the method is not void, it must have areturnstatement returning an integer value likereturn product;.Final Answer:
Missing return statement for non-void method. -> Option CQuick Check:
Non-void methods must return a value [OK]
- Forgetting to return a value in non-void methods
- Thinking variable declaration is invalid inside methods
- Assuming parameters must be strings
Solution
Step 1: Understand method purpose and return type
The method should print a message and not return any value, so it should bevoid.Step 2: Check each option for correctness
public void Greet() { Console.WriteLine("Hello!"); } correctly usesvoidand prints the message without returning anything. public string Greet() { Console.WriteLine("Hello!"); return "Hello!"; } returns a string, which is not needed. public void Greet() { return "Hello!"; } tries to return a string in a void method (invalid). public string Greet() { Console.WriteLine("Hello!"); } declares string return type but does not return anything (invalid).Final Answer:
public void Greet() { Console.WriteLine("Hello!"); } -> Option BQuick Check:
Void method prints only, no return [OK]
- Returning a value from a void method
- Declaring return type but not returning
- Confusing printing with returning
Ready to prove it?
Pass the test and this pattern moves unknown
