Underlying numeric values in C Sharp (C#) - Time & Space Complexity
We want to understand how the time to get the numeric value behind a number type grows as the input changes.
How does the work change when we access or convert underlying numeric values?
Analyze the time complexity of the following code snippet.
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
int value = numbers[i];
int doubled = value * 2;
}
This code loops through an array of numbers, accesses each number, and doubles it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing each element in the array and multiplying it.
- How many times: Exactly once for each element, so n times.
As the number of elements grows, the work grows in a straight line with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times accessing and doubling |
| 100 | About 100 times accessing and doubling |
| 1000 | About 1000 times accessing and doubling |
Pattern observation: The work increases evenly as the input size increases.
Time Complexity: O(n)
This means the time to get and use each numeric value grows directly with the number of values.
[X] Wrong: "Accessing the underlying numeric value is instant and does not depend on the number of elements."
[OK] Correct: While accessing one value is quick, doing it many times adds up, so the total time grows with the number of elements.
Understanding how simple operations like accessing numeric values scale helps you reason about bigger problems and write efficient code.
"What if we replaced the array with a linked list? How would the time complexity change when accessing underlying numeric values?"
Practice
Solution
Step 1: Understand default enum numbering
By default, C# assigns the first enum member the value 0.Step 2: Check subsequent members
Each following member increments by 1 automatically unless explicitly set.Final Answer:
It starts at 0 and increments by 1 for each member -> Option AQuick Check:
Default enum values start at 0 and increment [OK]
- Thinking enums start at 1 by default
- Assuming enum values are random
- Believing manual assignment is always required
Day.Monday in C#?Solution
Step 1: Recall casting enum to int
In C#, you get the numeric value by casting the enum member to int using (int).Step 2: Check other options
Enums have no ToInt() method or Value property; Convert.ToInt32(Day.Monday.ToString()) parses the name ("Monday") and throws FormatException.Final Answer:
int value = (int)Day.Monday; -> Option DQuick Check:
Cast enum to int with (int) [OK]
- Using non-existent methods like ToInt()
- Trying to access a Value property
- Converting enum.ToString() to int (parses name)
enum Color { Red = 3, Green, Blue }What is the output of this code?
Console.WriteLine((int)Color.Green);
Solution
Step 1: Identify explicit enum value
Color.Red is set to 3 explicitly.Step 2: Understand implicit increment
Color.Green is next, so it gets 4 (3 + 1).Final Answer:
4 -> Option AQuick Check:
Enum increments from explicit value [OK]
- Assuming Green is 1 by default
- Thinking Green equals Red's value
- Forgetting implicit increment after explicit value
enum Status { Active = 1, Inactive = 1, Pending = 2 }Solution
Step 1: Check enum value rules
C# allows enum members to share the same underlying numeric value.Step 2: Verify other options
Enum members can be numbers, do not need sequential values, and duplicates are allowed.Final Answer:
Nothing is wrong; duplicate values are allowed -> Option BQuick Check:
Duplicate enum values are valid in C# [OK]
- Thinking duplicate enum values cause errors
- Believing enums must start at 0 and be sequential
- Confusing enum members with strings
enum Level { Low = 1, Medium = 5, High = 10 }Which code correctly checks if an integer variable
score matches any enum value?Solution
Step 1: Understand Enum.IsDefined usage
Enum.IsDefined checks if a given integer matches any enum member's underlying value.Step 2: Analyze other options
A checks range (includes invalid like 2); B casts int to enum (always succeeds) != null (always true); C direct int == enum comparisons do not compile without casting enum to int.Final Answer:
bool isMatch = Enum.IsDefined(typeof(Level), score); -> Option CQuick Check:
Use Enum.IsDefined to check valid enum values [OK]
- Casting int to enum without validation
- Checking range instead of exact enum values
- Comparing int directly to enum members without casting
Ready to prove it?
Pass the test and this pattern moves unknown
