Thursday, August 28, 2014

C# Tuple

Intro


What's a Tuple? In the world of .Net, it's a somewhat-generic structure that can hold a number of elements that you define upon declaration/creation. Yeah that's a helpful description, I know. I aim to please! I really can't think of much else to say about these things that will clear up the fog of war, so let's just see a quick sample:

        private Tuple<int, string, int> DoStuffWithNumbers(int num1, int num2)
        {
            int product = num1 * num2;
            string description = "i've done crazy things with numbers";
            int modulus = num1 % num2;
            return new Tuple<int, string, int>(product, description, modulus);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var thing = DoStuffWithNumbers(84, 9);
            Response.Write(thing.Item1 + "::" + thing.Item2 + "::" + thing.Item3);
        }



The above method DoStuffWithNumbers needs to return 3 values. Sure, we could do that with 3 out parameters, but that's a pain! I could also declare a class, maybe call it StuffWithNumbersResult, and give it 3 properties; but yeah, I'm still just as lazy as I was last week so I don't want to create a new class just for that. In comes the Tuple. The method just returns an object (a Tuple) with 3 properties; an int, a string, and another int. The Page_Load method calls DoStuffWithNumbers in the first line, then in the 2nd line it accesses the Tuple via properties Item1, Item2, and Item3. It's not the prettiest way to accomplish things, but it's at least simple. And hey, you still get strongly-typed variables (the method-scope variable "thing" is strongly typed as Tuple<int, string, int>), and you have less coding than you might otherwise.


Why, and Why Not?

The why should be pretty easy to figure out from the above sample. It saves you some coding and by extension a little bit of program complexity. The why not should also be easy to discover. What does thing.Item1 mean? You have to dig into the code to find out, rather than having something more meaningful. For example, if you had declared a StuffWithNumbers class with properties Product, Description, and Modulus, that's a lot more readable than thing.Item1-3. Thus, your code is probably just a wee bit harder to maintain, especially if it's not you doing the maintenance.

Anything Else?

Why of course there's more to it! There's a slightly easier syntax for creating your Tuple which I'm about to show you:

return Tuple.Create(product, description, modulus);

This here line of code can be used in place of the last line from DoStuffWithNumbers() from up above. It's just a slightly simpler way of creating your Tuple without having to use the angle-bracket syntax of generics.

That's about it! Happy Tupling...

What's Next?

Nothing else to say really. Check out the link below though for a few nitpicky details about Tuples. I didn't cover everything, but I covered the fun stuff.

And hey, with great coding power comes great responsibility to future devs who have to maintain your work. Translation: Use Tuples sparingly. If used in excess, they can make your code pretty tough to follow.

Resources
Tuple Class (System)

No comments:

Post a Comment