use the name of the field instead of index in list array in C#

后端 未结 3 1502
盖世英雄少女心
盖世英雄少女心 2021-01-28 17:13

I have a getvalue object that contains a price list which consists of 5 items. I need to get the value of one of the elements. I can get the value by index:

<
3条回答
  •  没有蜡笔的小新
    2021-01-28 17:34

    You could do this by adding an indexer property to the ValuationPrices type.

    public ValuationPrice this[string name]
    {
        get
        {
            return this.First(n => n.Name == value);
        }
    }
    

    Then you would be able to write getvalue1.ValuationPrices["fieldName"].

    The implementation of the indexer property will vary depending on the internal structure of your classes, but hopefully this gives you some idea of the syntax used to implement the indexer.

提交回复
热议问题