{"id":5616,"date":"2017-12-15T07:41:28","date_gmt":"2017-12-15T07:41:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5616"},"modified":"2017-12-15T07:41:28","modified_gmt":"2017-12-15T07:41:28","slug":"c-7-tuples","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-7-tuples\/","title":{"rendered":"C# 7 Tuples"},"content":{"rendered":"<p>C#\/.NET has had support for the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.tuple(v=vs.110).aspx\" rel=\"noopener\" target=\"_blank\">Tuple class<\/a> (a reference type) since .NET 4, but with C# 7 tuples now gets some &#8220;syntactic sugar&#8221; (and a new <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.valuetuple(v=vs.110).aspx\" rel=\"noopener\" target=\"_blank\">ValueTuple<\/a> struct) to make them part of the language.<\/p>\n<p>Previous to C# 7 we&#8217;d write code like this <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar tuple = new Tuple&lt;string, int&gt;(&quot;Four&quot;, 4);\r\n\r\nvar firstItem = tuple.Item1;\r\n<\/pre>\n<p>Now, in a style more like F#, we can write the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar tuple = (&quot;Four&quot;, 4);\r\n\r\nvar firstItem = tuple.Item1;\r\n<\/pre>\n<p>This code makes the creation of tuples slightly simpler but we can also make the use of the &#8220;items&#8221; within a tuple more readable by using names. So we could rewrite the above in a number of ways, examples of each supplied below<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n(string name, int number) tuple1 = (&quot;Four&quot;, 4);\r\nvar tuple2 = (name: &quot;Four&quot;, number: 4);\r\nvar (name, number) =  (&quot;Four&quot;, 4);\r\n\r\nvar firstItem1 = tuple1.name;\r\nvar firstItem2 = tuple2.name;\r\nvar firstItem3 = name;\r\n<\/pre>\n<p>Whilst not quite as nice as F# we also now have something similar to pattern matching on tuples, so for example we might be interested in creating a switch statement based upon the numeric (second item) in the above code, we can write<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nswitch (tuple)\r\n{\r\n   case var t when t.number == 4:\r\n      Console.WriteLine(&quot;Found 4&quot;);\r\n      break;\r\n}\r\n<\/pre>\n<p>Again, similar to F# we also can <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/discards\" rel=\"noopener\" target=\"_blank\">discard<\/a> items\/params using the underscore _, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar (_, number) = CreateTuple();\r\n<\/pre>\n<p>In this example we assume the <em>CreateTuple<\/em> returns a tuple type with two items. The first is simply ignored (or discarded).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\/.NET has had support for the Tuple class (a reference type) since .NET 4, but with C# 7 tuples now gets some &#8220;syntactic sugar&#8221; (and a new ValueTuple struct) to make them part of the language. Previous to C# 7 we&#8217;d write code like this var tuple = new Tuple&lt;string, int&gt;(&quot;Four&quot;, 4); var firstItem = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-5616","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5616"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5616\/revisions"}],"predecessor-version":[{"id":5652,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5616\/revisions\/5652"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}