I am still new to C# and many things about it don't make sense to my historically C++ brain. For instance, I need to make a List
of Tuples
of Tuples
. I would do this with a typedef
in C++, but I see that this doesn't really exist as such in C#. I find this annoying, but more importantly, I have this terrible feeling that I am doing it the wrong way, especially in the way I 'overload' the Add
method.
using System;
using System.Collections.Generic;
namespace MyTypeDefs
{
internal class RangesAndCoeffs
{
private List<Tuple<Tuple<int, int>, Tuple<double, double>>> _rangesAndCoeffs;
public RangesAndCoeffs()
{
_rangesAndCoeffs = new List<Tuple<Tuple<int, int>, Tuple<double, double>>>();
}
public void Add(Tuple<int, int> range, Tuple<double, double> coeffs)
{
_rangesAndCoeffs.Add(new Tuple<Tuple<int, int>, Tuple<double, double>>(range, coeffs));
}
public Tuple<double, double>? GetCoeffsFromIndex(int index)
{
foreach (var rangeAndCoeffs in _rangesAndCoeffs)
{
if (index > rangeAndCoeffs.Item1.Item1 && index < rangeAndCoeffs.Item1.Item2)
return rangeAndCoeffs.Item2;
}
return null;
}
}
}
I have the sneaking suspicion that there is a more elegant and safer way to do this. This is an internal class, so it doesn't have to be water tight, but I definitely need to avoid typing List<Tuple<Tuple<int, int>, Tuple<double, double>>>
1000 times.
I know this is rather vague, so my apologies to the SO gurus. I'm really just looking for some style tips and couldn't find anything online. Any pointers to related articles are most welcome.
Incidentally, I got some interesting ideas about this here: (Why not inherit from List<T>?), but it is a slightly different question.