Skip to content

Instantly share code, notes, and snippets.

@ohlawdie
Last active February 11, 2020 22:47
Show Gist options
  • Save ohlawdie/75adf0f86bd07ceb15a0be2ca76eef60 to your computer and use it in GitHub Desktop.
Save ohlawdie/75adf0f86bd07ceb15a0be2ca76eef60 to your computer and use it in GitHub Desktop.
tuple #note
// With identity conversions you may have generic variance in mind, however, this is not possible due to
// Identity conversions are mostly important for tuples when it comes to constructed types. If you could easily convert from (int, int) to (int x, int y) but not from IEnumerable<(int, int)> to IEnumerable(<int x, int y>) you would be quite annoyed.
//Although you can ignore some preceding information and ignore what the CLR does with tuples, you'll be able to do more with tuples and better understand the behavior.
//Unlike anonymous types, in which unique sequence of property names within an assembly causes the compiler to generate a new type, typles don't require any extra types to be generated by the compiler. Instead, it uses a new set of types from the framework.
//Tuples in C# 7are implemented using the System.ValueTuple. A description of any type of is very much like the description of tuple types from earlier: it's a value type with public fields. You might expect the ValueTuple to be static - it is not.
//Compiler generated
var tuple = new ValueTuple<int, int>(10, 20);
tuple.Item1;
tuple.Item2;
//Element names exist for tuples within the C# language, but not in the CLR.
tidbits: the arity-8 tuple's final field is called Rest. Skeet calls this "womple" As far as generics...generic types are generic only in their type parameters, you can't give two constructed types different field names.
//So we a womple and a nuple...a non-generic tuple. Nuples are useful for pattern matching and decomposition but they are currently just placeholder at the moment.
//System.TupleExtensions
//Deconstruct, which extends the Tuple types
//ToValueType, which also extends the Tuple types
//ToTuple, which extends the ValueTuple types
//REMEMBER, copying a reference is atomic, whereas copying a value tuple is not!
//Lastly, tuples and dynamic do not play nicely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment