[API Proposal]: Add indexer to record #7705
-
Background and motivationI suggest adding an indexer to the record for all fields so that the runtime can automatically generate code for it, similar to how it is done for ToString API Proposal public record JsonSaveModel
{
public object this[string obj]
{
set {
if (obj == nameof(index))
index = value;
throw new Exception();
}
get {
if (obj == nameof(index))
return index;
throw new Exception();
}
}
public string index { get; set; }
} API Usage class Q
{
public Q()
{
var res = new JsonSaveModel();
res["index"] = "90239023";
}
} Alternative DesignsNo response RisksNo response |
Beta Was this translation helpful? Give feedback.
Answered by
CyrusNajmabadi
Nov 18, 2023
Replies: 2 comments 8 replies
-
This should be a language proposal instead of an API proposal. |
Beta Was this translation helpful? Give feedback.
0 replies
-
This sounds like a good use case for a source generator. I don't think indexers as you've described would be a generally desirable feature. |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Mr0N you write the source generator once. Then you can use it in every project.