What if your methods could tell you exactly what they did, instead of leaving you guessing?
Why Return values and void methods in C Sharp (C#)? - Purpose & Use Cases
Imagine you are baking cookies and writing down each step on paper. After baking, you want to know how many cookies you made, but you only wrote the steps, not the result.
Without a way to get results back, you must guess or check manually. This wastes time and can cause mistakes, especially if you repeat the process many times.
Return values let methods give back results, like telling you exactly how many cookies you baked. Void methods do tasks without returning anything, like just baking without counting.
void BakeCookies() { /* bake but no count returned */ }
int count = 0; // manual tracking outsideint BakeCookies() { /* bake and return count */ return 12; }It lets your program communicate results clearly, making code easier to use and understand.
A calculator method returns the sum of two numbers so you can use it elsewhere, while a void method might just print a message without returning anything.
Return values send results back from methods.
Void methods perform actions without returning data.
Using both properly makes programs clearer and more powerful.
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
