dotnet / csharplang

The official repo for the design of the C# programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[API Proposal]: Add indexer to record

Mr0N opened this issue · comments

commented

Background and motivation

I 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 Designs

No response

Risks

No response

This should be a language proposal instead of an API proposal.