Advanced Tuple Techniques in C#

 In Cloud Software Development, Genel

Tuple in C# was introduced at C# 4 but it was nothing like what some other languages has to offer. It was clunky and hard to use. The only thing they provided us in C# was a couple of generic classes and nothing more. But it is history now because as of C# 7, they are massively improved and have a first class support in the language. Today we are going to take a look at how to use them and after that I will show you some techniques on how to use them more 🙂

First, how to use them;

//You can have multiple return values using tuples new syntax.
(string hello, string world) GetHelloWorldMessage()
{
// And create tuple with having your values in paranthesis.
return ("Hello", "World");
}
//You can deconstruct the resulting tuples values and assing them to different variables like this.
var (h, w) = GetHelloWorldMessage();
Console.Write($"{h} {w}");
//You can also assing your variables with type declarations.
var (string h2, string w2) = GetHelloWorldMessage();
Console.Write($"{h} {w}"); // This has the same output.
//Of course you can assing resulting tuple to a single tuple variable and use it like this.
var t = GetHelloWorldMessage();
Console.Write($"{t.hello} {t.world}"); // This has the same output too.
// These tuples are actually using the new ValueTuple struct and this struct actually have the "ItemX" properties inside like the old tuple do.
// So you can use these properties too if you want.
Console.Write($"{t.Item1} {t.Item2}"); // Guess what, this has the same output.
view raw Tuples.cs hosted with ❤ by GitHub

So, if we are on the same level now, we can get in to the juicy details.

Add deconstruct support to your classes

Actually this one is not entirely related to tuples but makes sense afterwards. You can add a method called “Deconstruct” in your classes and imitate how tuples deconstruct itself.

public class Person
{
public Person(string name = null, string surname = null, int age = 0)
{
Name = name;
Surname = surname;
Age = age;
}
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public void Deconstruct(out string name, out string surname, out int age)
{
name = Name;
surname = Surname;
age = Age;
}
}
var person = new Person("John", "Doe", 65);
var (name, surname, age) = person; // This works because we have a "Deconstruct" method in our class.

Add implicit operator for that tuple to your classes

You can use your newly created tuples in your implicit operators because they are like your any other type. Let’s improve our previous example.

public class Person
{
public Person(string name = null, string surname = null, int age = 0)
{
Name = name;
Surname = surname;
Age = age;
}
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public void Deconstruct(out string name, out string surname, out int age)
{
name = Name;
surname = Surname;
age = Age;
}
public static implicit operator Person((string name, string surname, int age) x) => new Person(x.name, x.surname, x.age);
}
var person = new Person("John", "Doe", 65);
var (name, surname, age) = person; // This works because we have a "Deconstruct" method in our class.
var t = (name, surname, age); // We are creating a new tuple using existing variables.
Person p2 = t; // This works because we have an implicit operator for the (string, string, int) tuple types.

Write extension methods to or for your tuples

When you have no access to specific class or struct and want to add something but don’t want to create something new inheriting that, the answer is always writing an extension method. I heavily use them when I’m coding and I found a way to use them here too. You can write extension methods for the tuples or write something returns one.

public static class TupleExtensions
{
public static Point ToPoint(this (int x, int y) self) => new Point(x,y);
public static (int x, int y) => ToTuple(this Point self) => (self.X, self.Y);
}
// First, let's create a new point.
var p = new Point(10,20);
// And deconstruct it with converting it a tuple first with ToTuple extension method.
var (x,y) = p.ToTuple();
// At last, create a tuple using x and y variables and convert it to a point using ToPoint extension method.
var p2 = (x,y).ToPoint();

So, that’s it for now. Please let me know how you are using the new tuples in C# and I will make sure to add more if I find any other way to use them.

Thanks for tuning in and see you in the next post.

Önerilen Yazılar
İletişim

Start typing and press Enter to search