Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere should I put the associated methods of a List<T> of datarows?
    text
    copied!<p>Preface: read <a href="https://stackoverflow.com/questions/1192894/how-to-store-tables-in-c-what-kind-of-data-structure-should-i-use">this</a> question. Let's say that I have all my stuff in a data structure described by <a href="https://stackoverflow.com/questions/1192894/how-to-store-tables-in-c-what-kind-of-data-structure-should-i-use/1192912#1192912">Skeet</a>. 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.</p> <pre><code>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&lt;MeteoDataPoint&gt; list; public MeteoDataPointList() { list = new List&lt;MeteoDataPoint&gt;(); } // 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; } } </code></pre> <p>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?</p> <p><em>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.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload