tags:

views:

75

answers:

3

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.

+2  A: 

Just use a generic list of MeteoDataPoint whenever you need a list of them. Generics are found in System.Collections.Generic. You can find info on generic lists here.

var list = new List<MeteoDataPoint>();

To add additional methods, I would use extensions.

public static class MeteoDataPointExtensions
{
     // not really much point to this extension, but it demonstrates the idea
     public static double AverageTemp( this IEnumerable<MeteoDataPoint> items )
     {
         return items.Average( l => l.Temperature ); 
     }
}

Updated to use IEnumerable instead of list to make it more applicable to different types of collections.

tvanfosson
+1  A: 

I'm really confused as to what purpse MeteoDataPointList serves...?

And no; you don't need a destructor/finalizer unless you are handling unmanaged objects.

For example, it would be a lot simpler to just subclass Collection<T> (or perhaps List<T>) to extend it:

public sealed class MeteoDataPointList : Collection<MeteoDataPoint>
{
    // additional methods only, such as AverageTemperature
}

Note that in C# 3.0 you can also do this via extension methods, and it will be available on any common collection/sequence of MeteoDataPoints.

public static class MeteoExtension {
    public double AverageTemperature(this IEnumerable<MeteoDataPoint> items)
    {
        // or just return items.Average(m=>m.Temperature);  ;-p
        double sum=0;
        int count = 0;
        foreach (MeteoDataPoint m in items) { sum += m.Temperature; count++; }
        return sum / count;
    }
}
Marc Gravell
You don't have count on a IEnumerable. use a collection.
Guillaume
The only purpose of MeteoDataPointList was to help me bind methods to my list.
Richard J. Terrell
Using the IEnumerable in the extensions is a good idea.
tvanfosson
Fixed count... ;-p
Marc Gravell
+2  A: 

Yeah, I would think you'd want to subclass the List:

public class MeteoDataPointList : List<MeteoDataPoint>
{
    public MeteoDataPointList() { } 

    // and now I can wrtie my custom methods specific to my data
    public double AverageTemperature()
    {
        double sum=0;
        foreach (MeteoDataPoint m in this) { sum += m.Temperature;  }
        return sum / this.Count;
    }
}

No need to worry about a destructor. For C# you would look into implementing IDisposable, but this is mostly necessary for handling clean up of unmanaged resources (which you don't have in this case).

Also, there is already an extension method for an Average function, so you could technically just use:

List<MeteoDataPoint> list

and do:

double avg = list.Average(point => point.Temperature);

read about Extension Methods and Func Lambda Expressions are also good to know about

Mark Synowiec