Preface: read this question. Let's say that I have all my stuff in a data structure described by Skeet. Now I want to write some methods dealing with this kind of data. This is what I have come up with: Ideally this kind of data should be in its own class. So I have decided to wrap the List of datapoints.
public class MeteoDataPoint
{
private readonly DateTime timeStamp;
public DateTime TimeStamp { get { return timeStamp; } }
private readonly double temperature;
public double Temperature { get { return temperature; } }
private readonly double airPressure;
public double AirPressure { get { return airPressure; } }
public MeteoDataPoint(DateTime timeStamp,
double temperature,
double airPressure)
{
this.timeStamp = timeStamp;
this.temperature = temperature;
this.airPressure = airPressure;
}
}
public class MeteoDataPointList
{
private List<MeteoDataPoint> list;
public MeteoDataPointList() { list = new List<MeteoDataPoint>(); }
// expose some of List's functionality
public void Add(MeteoDataPoint p) { list.Add(p); }
public bool Remove(MeteoDataPoint p) { return list.Remove(p); }
public void RemoveAt(int i) { list.RemoveAt(i); }
public int Count { get { return list.Count; } }
public MeteoDataPoint this [int i] { get { return list[i]; } }
// and now I can wrtie my custom methods specific to my data
public double AverageTemperature()
{
double sum=0;
foreach (MeteoDataPoint m in list) { sum += m.Temperature; }
return sum / list.Count;
}
}
What do you think? How can I improve on this? Is there a better way (maybe subclassing List or just a class with static methods for manipulating a List of MeteoDataPoint)? Should I care about a destructor?
Note: Sorry if the question is too trivial, I have been doodling around with csharp, but this will be the first time I have to create something useful, and as the language provides so many ways to accomplish the same task I often find myself at crossroads wondering which way is the best.