akTed
akTed

Reputation: 250

Can you add two (int) tuples together?

Something like:

var Tuple1 = (x:2,y:4);
var Tuple2 = (x:0,y:-1);
var Tuple3 = Tuple1 + Tuple2;
// desired output: (x:2,y:3)

Is there a simple way, or must I do something like:

Tuple3.x = Tuple1.x + Tuple2.x;
Tuple3.y = Tuple1.y + Tuple2.y;

I'm also open to using another structure instead of tuple.

Upvotes: 2

Views: 119

Answers (3)

Guru Stron
Guru Stron

Reputation: 142903

Without any extra work the one-liner would be:

var tuple1 = (x:2,y:4);
var tuple2 = (x:0,y:-1);
var tuple3 = (x: tuple1.x + tuple2.x, y: tuple1.y + tuple2.y);

Another option is to create a extension method Add (this method leverages two other features - generics and generic math, the latter being available since .NET 7):

public static class TupleExts
{
    public static (TX X, TY Y) Add<TX, TY>(this (TX X, TY Y) left, (TX X, TY Y) right)
        where TX : IAdditionOperators<TX, TX, TX>
        where TY : IAdditionOperators<TY, TY, TY> =>
        (left.X + right.X, left.Y + right.Y);
}

And usage:

var tuple1 = (x:2,y:4);
var tuple2 = (x:0,y:-1);
var tuple3 = tuple1.Add(tuple2);

But since you wrote that you are not limited to tuples then you can define your own type and overload the + operator. See the Operator overloading documentation. Something to start with:

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public static Point operator +(Point left, Point right) => new Point
    {
        X = left.X + right.X,
        Y = left.Y + right.Y
    };
}

Then you will be able to use the + operator:

var p = new Point { X = 2, Y = 4 } + new Point { X = 0, Y = -1 };

Upvotes: 7

IV.
IV.

Reputation: 9417

Are you doing this one time? I can't really offer anything that would "save you typing".

But, if this is something you're going to do a lot then I suggest making an Extensions like Add() and Sum() to do this in a straightforward, reusable way.

static class Extensions
{
    // Sum a second tuple with a first.
    public static (int x, int y) Add(this (int x, int y) tuple1, (int x, int y) tuple2) =>
        (tuple1.x + tuple2.x, tuple1.y + tuple2.y);

    // Sum an array of specific tuples
    public static (int x, int y) Sum(this (int x, int y)[] tuples) =>
        tuples.Aggregate((0, 0), (sum, next) => sum.Add(next)); 

}

Test using Console App

Console.Title = "Fun with Tuples";
var Tuple1 = (x: 2, y: 4);
var Tuple2 = (x: 0, y: -1);

var SumOfTwoTuples = Tuple1.Add(Tuple2);

Console.WriteLine($"SumOfTwoTuples: {SumOfTwoTuples}" );

var Tuple3 = (x: 3, y: 6);
var Tuple4 = (x: -2, y: 5);
var Tuple5 = (x: -10, y: -3);

var SumOfThreeTuples =
    Tuple3
    .Add(Tuple4)
    .Add(Tuple5);

Console.WriteLine("\n// Fluent chaining");
Console.WriteLine($"SumOfThreeTuples: {SumOfThreeTuples}");


Console.WriteLine("\n// Arrays");
var arrayOfTwoTuples = new[] { Tuple1, Tuple2 };
Console.WriteLine($"SumOfTwoTupleArray: {arrayOfTwoTuples.Sum()}");

var arrayOfThreeTuples = new[] { Tuple3, Tuple4, Tuple5 };
Console.WriteLine($"SumOfThreeTupleArray: {arrayOfThreeTuples.Sum()}");

Console.ReadKey();

console output


In your comment, you said:

I'm only a couple months into my C# journey

I predict that once you write your first extension method, "your life will never be the same".

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416049

There's nothing built-in for Tuples to support this natively. You will have to write the code yourself.

But you can hide the code in an extension method if you want:

public static (int, int) Add(this (int, int) t1, (int, int) t2) =>  (t1.Item1+t2.Item1, t1.Item2+t2.Item2);

Then you can do this:

var Tuple3 = Tuple1.Add(Tuple2);

Obviously this won't scale well to support tuples of other types or length.

Upvotes: 2

Related Questions