0

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.

dmedine
  • 1,430
  • 8
  • 25
  • You want an POD? It's the same name "struct". Give `Tuple` and `Tuple` better names as a struct as well. – Louis Go Mar 31 '20 at 09:16
  • 5
    Frankly, I wouldn't use `Tuple<,>` here; it is *not* expressive, nor is it efficient; you *could* use a value-tuple, i.e. `List<((int x, int y), (double a, double b))>`, but frankly I'd probably declare some `readonly struct` types and use *those* instead, i.e. `List` – Marc Gravell Mar 31 '20 at 09:16
  • @Marc Gravell, actually I suppose your solution is even more C++ish and I will try it out. One question is how do I know what C# assemblies are 'efficient' or not? I have read that ```List``` is highly optimized, but what's wrong with ```Tuple```? Is not ```Tuple``` itself nothing but a templated ```readonly struct```? – dmedine Mar 31 '20 at 23:21
  • @dmedine no; the `Tuple<...>` family are *classes*; `ValueTuple<...>` are structs; the language support i.e. `(int a, string b)` or just `(int, string)` is specifically `ValueTuple<...>`; also, .NET doesn't have templates - it has generics; similar but different – Marc Gravell Apr 01 '20 at 07:48

0 Answers0