Return values and void methods in C Sharp (C#) - Time & Space Complexity
Let's see how the time it takes to run a method changes depending on what the method does.
We want to know how the method's work grows as input changes.
Analyze the time complexity of the following code snippet.
public int SumArray(int[] numbers)
{
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
return sum;
}
public void PrintHello()
{
Console.WriteLine("Hello!");
}
This code has two methods: one adds up all numbers in an array and returns the total, the other just prints a message and returns nothing.
Look for loops or repeated steps.
- Primary operation: The for-loop in
SumArraythat goes through each number. - How many times: It runs once for every item in the input array.
- PrintHello method: No loops, just one print action.
As the array gets bigger, the method does more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of items. More items mean more additions.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "Since PrintHello doesn't return anything, it must take longer than SumArray."
[OK] Correct: PrintHello just does one action, so it takes the same time no matter what. The return type (void or int) doesn't affect how long it runs.
Understanding how loops affect time helps you explain your code clearly and shows you know how programs grow with input size.
What if we changed the SumArray method to call another method inside the loop? How would the time complexity change?
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
