0

I have:

public class Schedule
{
    public List<DateTime> Dates {get; set;}

// Some methods
}

I'd like Schedule to behave as List:

var date = Schedule[idx];
dates = List<DateTime>;
Schedule = dates;
dates = Schedule;
...

Schedule should be a list of Dates as it is in reality, by adding a member Dates, it feels like a computer object, adding a layer of abstraction and moving away from the modeled object.

There are 2 possibilities but they are not perfect:

Overloading operators (e.g. "[]") but "="can't be overloaded.

Alias with "using" but it won't have any method.

For example, in C++, I would have overloaded the operators and keep a private member _dates.

Thanks

isidore12
  • 21
  • 5
  • 3
    "Schedule should be a list of Dates as it is in reality, by adding a member Dates, it feels like a computer object, adding a layer of abstraction and moving away from the modeled object." ... What? Huh? – DigitalNinja Jun 13 '15 at 00:52
  • Assuming your actual question is about getting `Schedule = dates` to work - covered in existing ["Overloading assignment operator in C#"](http://stackoverflow.com/questions/4537803/overloading-assignment-operator-in-c-sharp). If you mean something else - please clarify how your requirement is different. – Alexei Levenkov Jun 13 '15 at 01:08

1 Answers1

3

If Schedule IS a list of dates, then make it one:

public class Schedule : List<DateTime>
{
   // More stuff
}

Schedule MySchedule = new Schedule();

MySchedule.Add(DateTime.Now);
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69