V
V
Victor P.2022-03-24 13:54:21
C++ / C#
Victor P., 2022-03-24 13:54:21

How to write wrapper classes over collections in c# so that access to the collection is by class name?

Hello,
Let's say there is a Prices class, inside of which one collection with price history is encapsulated

public class Record
{
  public DateTime Date { get; set; }
  public MenuTypeEnum MenuType { get; set; }
  public decimal? Price { get; set; }
}

public class Prices
{
  public Record[] Records { get; private set; }

  public Record GetCurrentVersion()
  {
    return this.Records.OrderByDescending(x=>x.Date).FirstOrDefault();
  }
}


Accordingly, when working with such a class, an object is created and access to the collection is carried out through the dot
var price = new Prices { /передаём коллекцию/ };
var historyCount = price.Records.Count();


There was some kind of syntax so that you could work with the collection immediately by the class name, so as not to prescribe each method inside the class, but use the existing methods to work with collections by the class name:
var price = new Prices { /передаём коллекцию/ };
var historyCount = price.Count();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor P., 2022-03-24
@Jeer

Found, this section is called "indexers" metanit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question