Questions tagged [indexer]
367 questions
329
votes
4 answers
How do I overload the [] operator in C#
I would like to add an operator to a class. I currently have a GetValue() method that I would like to replace with an [] operator.
class A
{
private List values = new List();
public int GetValue(int index) => values[index];
}

Adam Tegen
- 25,378
- 33
- 125
- 153
142
votes
7 answers
Static Indexers?
Why are static indexers disallowed in C#? I see no reason why they should not be allowed and furthermore they could be very useful.
For example:
public static class ConfigurationManager
{
public object this[string name]
{
…

Malfist
- 31,179
- 61
- 182
- 269
50
votes
4 answers
Implementing an indexer in a class in TypeScript
Is it currently possible to implement an indexer on a class in TypeScript?
class MyCollection {
[name: string]: MyType;
}
This doesn't compile. I can specify an indexer on an interface, of course, but I need methods on this type as well…

MgSam
- 12,139
- 19
- 64
- 95
49
votes
14 answers
Real world use cases for C# indexers?
I've seen lot of examples for c# Indexers, but in what way will it help me in real life situations.
I know the C# guru wouldn't have added this if it wasn't a serious feature, but i cant think of a real world situation (not the foo bar stuff) to use…

Vivek Bernard
- 2,063
- 3
- 26
- 43
48
votes
8 answers
Eclipse CDT indexer does not know C++11 containers
I configured a C++11 project in Eclipse CDT to use gcc-4.7. It is not the default compiler on my system, which does not support C++11. In order for compilation to work, I need to pass the flag -std=c++11 and also include the following header path:…

clstaudt
- 21,436
- 45
- 156
- 239
39
votes
4 answers
PropertyChanged for indexer property
I have a class with an indexer property, with a string key:
public class IndexerProvider {
public object this[string key] {
get
{
return ...
}
set
{
...
}
}
…

Inferis
- 4,582
- 5
- 37
- 47
37
votes
4 answers
How to write a class that (like array) can be indexed with `arr[key]`?
Like we do Session.Add("LoginUserId", 123);
and then we can access Session["LoginUserId"], like an Array, how do we implement it?

Riz
- 6,746
- 16
- 67
- 89
30
votes
3 answers
Class with indexer and property named "Item"
Is it possible to create a class in .NET 4 with:
an indexer,
a property named "Item"?
For example, this C# class will not compile for me:
public class MyClass
{
public object Item { get; set; }
public object this[string index] { get {…

Michael
- 3,099
- 4
- 31
- 40
27
votes
4 answers
What does an exclamation mark in array index do?
While perusing through my organization's source repository I came across this little gem:
RawParameterStorage[!ParameterWorkingIdx][ParameterDataOffset] = ...
Is this valid code? (It compiles) What does the exclamation mark here do?
An invert ~…

Jim Fell
- 13,750
- 36
- 127
- 202
26
votes
1 answer
How can you bind an Indexed property to a control in WPF
Given an instance of the class ThisClassShouldBeTheDataContext as the Datacontext for the view
class ThisClassShouldBeTheDataContext
{
public Contacts Contacts {get;set;}
}
class Contacts
{
public IEnumerable Persons {get;set;}
public…

Lance
- 2,774
- 4
- 37
- 57
24
votes
2 answers
Extension Methods for Indexers, would they be good?
Extension Methods for Indexers, would they be good ?
I was playing around with some code that re-hydrates POCO's.
The code iterates around rows returned from a SqlDataReader and and uses reflection to assign properties from column values. Down my…

judek
- 313
- 2
- 8
23
votes
10 answers
When should you use C# indexers?
I'd like to use indexers more, but I'm not sure when to use them. All I've found online are examples that use classes like MyClass and IndexerClass.
What about in a school system where there are Students and Teachers, and each Teacher has a list of…

Ian Davis
- 19,091
- 30
- 85
- 133
22
votes
9 answers
Why doesn't Array class expose its indexer directly?
something to mention for answering:
Don't worry about variance, while the item in question is Array rather than T[].
A similar case for multi-dimension arrays is [here]
That is, N-dims to linear transform, is always possible. So this question…

Ken Kin
- 4,503
- 3
- 38
- 76
20
votes
3 answers
Using Moq to set indexers in C#
I'm having trouble figuring out how to set indexers in C# with Moq. The Moq documentation is weak, and I've done a lot of searching... what I'd like to do is similar in the solution to How to Moq Setting an Indexed property:
var someClass = new…

sourcenouveau
- 29,356
- 35
- 146
- 243
20
votes
2 answers
Creating a setter method that takes extra arguments in Ruby
I'm trying to write a method that acts as a setter and takes some extra arguments besides the assigned value. Silly example:
class WordGenerator
def []=(letter, position, allowed)
puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at…

Valentin Milea
- 3,186
- 3
- 28
- 29