Readonly indexer and constructor usage: question
See original GitHub issueclass A {
readonly x;
readonly [x: string]: string;
constructor() {
this.x = 5; // that's okay
this["a"] = "s"; // that yields an error
}
}
Is that by design that readonly indexers for the same type in constructor yield errors?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Can I make a read-only indexer in C#? - Stack Overflow
A read-only Indexer can be achieved by not including a set property in the declaration of the Indexer. To modify the Microsoft example....
Read more >Declare and Initialize Read-only Auto Properties in C# ...
You have initialized the class with the help of the constructor, and the fields were changed with the help of the property accessors....
Read more >Expression-Bodied Members in C# - GeeksforGeeks
The following example defines a Boolean method called IsEven() that ... The indexer is read-only, therefore the languages in the array ...
Read more >Expression-bodied members - C# Programming Guide
Method; Read-only property; Property; Constructor; Finalizer; Indexer. Methods. An expression-bodied method consists of a single expression that ...
Read more >Solved NOTE: I need full code in C# with outputs for - Chegg
Define class Student: (1) static and readonly field: (2) non-static/instance field: (3) Constructors Γ nverloading 1 \#\# for the above indexers, we need...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My use case is constructing a frozen array-like:
this[i] = value
obviously doesn’t work with the current rules, and I don’t understand the reasoning:An index signature defines unspecific extra properties, and the constructor is the place to set them if they’re
readonly
, because the object is created in the constructor so it’s local mutation.I’d do this before the assignment:
The link being there to document why it’s done.