Top-level statements in modern C# - Time & Space Complexity
We want to understand how the time it takes to run a program with top-level statements changes as the program grows.
Specifically, how does adding more code or operations affect the running time?
Analyze the time complexity of the following top-level statements code.
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += i;
}
Console.WriteLine(sum);
This code sums numbers from 0 up to n-1 and prints the result using top-level statements.
Look for loops or repeated actions.
- Primary operation: The for-loop that adds numbers.
- How many times: It runs once for each number from 0 to n-1, so n times.
As n gets bigger, the loop runs more times, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the running time grows in a straight line with the size of the input n.
[X] Wrong: "Top-level statements make the program run instantly regardless of input size."
[OK] Correct: Top-level statements just let you write code without extra wrapping, but the loop inside still runs n times, so time depends on n.
Understanding how loops inside top-level statements affect time helps you explain code efficiency clearly and confidently.
What if we replaced the for-loop with two nested for-loops each running n times? How would the time complexity change?
Practice
top-level statements in modern C#?Solution
Step 1: Understand top-level statements purpose
Top-level statements allow writing code directly without wrapping it inside a class or Main method.Step 2: Compare options with this feature
You can write code without explicitly defining a class or Main method. correctly states this benefit, while others contradict it or add incorrect restrictions.Final Answer:
You can write code without explicitly defining a class or Main method. -> Option DQuick Check:
Top-level statements remove boilerplate = A [OK]
- Thinking Main method is still required
- Believing top-level statements add more code
- Assuming it only works for complex apps
Solution
Step 1: Identify top-level statement syntax
Top-level statements allow writing code directly without class or method wrappers.Step 2: Check each option
Console.WriteLine("Hello, World!"); is direct code printing the message, valid as a top-level statement. Others require class or method definitions.Final Answer:
Console.WriteLine("Hello, World!"); -> Option AQuick Check:
Direct code without class = C [OK]
- Adding class or Main method unnecessarily
- Using incorrect method signatures
- Forgetting to call Console.WriteLine properly
int x = 5; int y = 10; Console.WriteLine(x + y);
Solution
Step 1: Understand variable initialization and addition
Variables x and y are integers with values 5 and 10. Adding them gives 15.Step 2: Check Console.WriteLine output
Console.WriteLine prints the sum of x and y, which is 15, not concatenation or error.Final Answer:
15 -> Option BQuick Check:
5 + 10 = 15 [OK]
- Thinking integers concatenate as strings
- Expecting variable names printed
- Assuming top-level causes errors
int number = 10 Console.WriteLine(number);
Solution
Step 1: Check syntax of variable declaration
The line 'int number = 10' is missing a semicolon at the end, which is required in C#.Step 2: Verify top-level statement rules
Variables can be declared and Console.WriteLine used directly in top-level statements, so other options are incorrect.Final Answer:
Missing semicolon after variable declaration. -> Option AQuick Check:
Every statement needs semicolon [OK]
- Thinking variables can't be declared top-level
- Assuming Console.WriteLine is disallowed
- Forgetting semicolon ends statements
Solution
Step 1: Check input reading and parsing
Console.WriteLine("Enter a number:"); int num = int.Parse(Console.ReadLine()); Console.WriteLine(num * 2); reads input as string, parses to int, then doubles it correctly.Step 2: Verify top-level usage and output
Console.WriteLine("Enter a number:"); int num = int.Parse(Console.ReadLine()); Console.WriteLine(num * 2); uses top-level statements correctly without class or Main method, and prints the doubled value.Final Answer:
Console.WriteLine("Enter a number:"); int num = int.Parse(Console.ReadLine()); Console.WriteLine(num * 2); -> Option CQuick Check:
Parse input to int before math [OK]
- Not parsing input string to int
- Using class and Main when top-level allowed
- Assigning Console.ReadLine() directly to int
Ready to prove it?
Pass the test and this pattern moves unknown
