I have some code where I first form trains (custom type Trains that contains an array of custom type Train). I then wish to add some data records to each object in the array that will be used later. Instead of writing a method I overloaded the operator +. I know this isn't necessarily the intended purpose for using such an operator overload but I liked the way the code looked!
Anyway now I'm back to feeling sensible and writing a method to do the same task. But its brought an important question to me. What's the difference between the following ways to achieve the same-ish result?
Overloading Approach
//Add Maintenance History Data (another custom type for storing such data records) to trains
trains += history;
public static Trains operator +(Trains txs, History[] hist)
{
foreach (Train t in txs)
{
t.AddHist(hist.First(x => x.ID == t.ID));
}
return txs;
}
Member Approach
trains.AddHist(history);
public void AddHistory(History[] hist)
{
foreach (Train t in Txs)
{
t.AddHist(hist.First(x => x.ID == t.ID));
}
}
I guess that there is a cost for assignment in the overload approach and therefore should stick to the member based approach?