5 ms·
There was always the Tuple class you could use since .Net 4. https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx https://msdn.microsoft.com/en
by guiomie 10y ago
There was always the Tuple class you could use since .Net 4.
https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.1...
- joshschreuder 10y agoThere's a few downsides to the Tuple class as mentioned here: https://github.com/dotnet/roslyn/issues/347 https://github.com/dotnet/roslyn/issues/347 Namely, heap allocation and the inability to name tuple members.
- cm2187 10y agoAnd a very verbose syntax. You have to repeat everywhere the different types of the different members.
- deleted 10y ago[deleted]
- ojanik 10y agoTuple.Create(3, "hello", true) Type inference FTW.
- andyjohnson0 10y agoI find that the verbosity can be minimised by type aliasing. E.g: using Complex = System.Tuple<double, double>; Edit: Clarity
- cm2187 10y agoBut then you might as well declare a class or structure to store the data. The point of tuples is to be self-contained.
- andyjohnson0 10y agoIt maybe wasn't clear, but I was referring to the parent's point about verbosity when using the 'old' Tuple class. using Complex = System.Tuple<double, double>; is less verbose than public class Complex { public double i { get; set; } public double j { get; set; } }
- douche 10y agoNot quite equivalent, since System.Tuple is immutable. So, at minimum: public class Complex { private readonly double _i; private readonly double _j; public double I {get {return _i;}} public double J {get{return _j;}} public Complex(double i, double j){ _i = i; _j = j; } } But System.Tuple is also IComparable, IStructuralComparable, IStructuralEquatable. I haven't had enough coffee yet to add all the boilerplate for that to the above, which only reinforces the point about verbosity.
- algorithmsRcool 10y agoAlthough with c# 6.0 (current release) you can simplify that to: public class Complex { public double I { get; } public double J { get; } public Complex(double i, double j) { I = i; J = j; } }
- doodpants 10y agoEven before C# 6.0, you could simplify it this way: public class Complex { public double I { get; private set; } public double J { get; private set; } public Complex(double i, double j) { I = i; J = j; } }