C# Tip 💡 How often do you use tuples in C#? Tuples allow you to define a lightweight data structure with ease. Some of the features of tuples you need to know are: • Value type • Deconstruction support • Strong typing • Item naming support
So here's a nice use case for tuples, which you can see in the image. Instead of defining out parameters, you can return multiple values from a method by creating a tuple.
@mjovanovictech Every time I see a tuple used as a return type I cry a bit 😅
@mjovanovictech When I decided I wasn't using tuples enough, I created this 😂github.com/yuretz/yTuple
@mjovanovictech I like to know that tuples exist, but to be honest, I feel that they give a bad smell to the code (e.g these fields should actually be encapsulated in a small class / object). Readability is also important. I guess I will only use tuples if it can solve performance issues.
@mjovanovictech Check out OneOf for more advanced scenarios that need type-safe switching on the result. github.com/mcintyre321/On…
@mjovanovictech This was one of the primary language features that unlocked async and out variables for me. I use it on the regular! With these, you can do very succinct if statements as well with what was traditionally Try(.. out).
@mjovanovictech I hate tuples. They make reading code very difficult. Only good use for them is in destructing for pattern matching