Indexer declaration in C Sharp (C#) - Time & Space Complexity
When we use an indexer in C#, we want to quickly get or set items by position.
We ask: how does the time to access an item change as the collection grows?
Analyze the time complexity of this indexer code.
public class SimpleList
{
private int[] items = new int[100];
public int this[int index]
{
get { return items[index]; }
set { items[index] = value; }
}
}
This code defines an indexer to get or set an element in an array by its position.
Look for repeated actions when using the indexer.
- Primary operation: Accessing an array element by index.
- How many times: Each access is a single direct operation, no loops involved.
Accessing an element by index takes the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to get or set an item stays constant as the collection grows.
Time Complexity: O(1)
This means accessing or setting an item by index takes the same short time no matter how many items there are.
[X] Wrong: "Accessing an item by index takes longer if the list is bigger."
[OK] Correct: Arrays allow direct access, so the time does not grow with size.
Understanding that indexers provide quick access helps you explain how collections work efficiently in real programs.
"What if the indexer accessed a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand what an indexer does
An indexer lets you access elements in an object using square brackets, similar to arrays.Step 2: Compare options with this definition
Only To allow objects to be accessed like arrays using square brackets describes this behavior correctly.Final Answer:
To allow objects to be accessed like arrays using square brackets -> Option BQuick Check:
Indexer = access with [] [OK]
- Confusing indexers with methods
- Thinking indexers create new objects
- Mixing indexers with constants
Solution
Step 1: Recall indexer syntax
An indexer uses the keywordthiswith a parameter list in square brackets and definesgetand/orsetaccessors inside braces.Step 2: Check each option
public int this[int index] { get; set; } matches the correct syntax exactly. public int indexer(int index) { get; set; } uses a method-like name, which is incorrect. public int this[index] { get; set; } misses the type in the parameter. public int this[int index] get set; misses braces and semicolons.Final Answer:
public int this[int index] { get; set; } -> Option DQuick Check:
Indexer syntax = this[type param] { get; set; } [OK]
- Using method name instead of 'this'
- Omitting parameter type
- Missing braces around get/set
class Sample {
private int[] data = {10, 20, 30};
public int this[int i] {
get { return data[i]; }
set { data[i] = value; }
}
}
var s = new Sample();
s[1] = 50;
Console.WriteLine(s[1]);Solution
Step 1: Understand the indexer behavior
The indexer accesses the private arraydata. Settings[1] = 50;changesdata[1]from 20 to 50.Step 2: Check the output of
This prints the updated value at index 1, which is 50.Console.WriteLine(s[1])Final Answer:
50 -> Option AQuick Check:
Indexer set changes value, output = 50 [OK]
- Assuming original value prints
- Confusing indexer with method call
- Expecting compilation error
public string this[int index]
{
get { return data[index]; }
set data[index] = value;
}Solution
Step 1: Review accessor syntax
Bothgetandsetaccessors must have braces enclosing their bodies.Step 2: Locate the error
Thesetaccessor lacks braces arounddata[index] = value;, causing a syntax error.Final Answer:
Missing braces around set accessor body -> Option CQuick Check:
Accessor bodies need braces { } [OK]
- Omitting braces in set accessor
- Wrong return type assumption
- Thinking parameter name is restricted
Solution
Step 1: Understand requirements for read/write access
The indexer must have bothgetandsetaccessors to allow reading and writing by index.Step 2: Evaluate each option
public string this[int i] { get { return names[i]; } } only hasget, so no writing. public string this[int i] { set { names[i] = value; } } only hasset, so no reading. public string this[int i] { get { return names[i]; } set { names[i] = value; } } has bothgetandsetwith int index parameter, which is correct. public string this[string name] { get { return name; } set { name = value; } } uses a string parameter, which does not match the requirement to access by index.Final Answer:
public string this[int i] { get { return names[i]; } set { names[i] = value; } } -> Option AQuick Check:
Read/write indexer needs both get and set [OK]
- Defining only get or only set
- Using wrong parameter type
- Confusing indexer parameter with property
Ready to prove it?
Pass the test and this pattern moves unknown
