{"id":88,"date":"2016-11-30T12:58:30","date_gmt":"2016-11-30T09:58:30","guid":{"rendered":"http:\/\/uysalyilmaz.com\/wordpress\/?p=88"},"modified":"2016-11-30T14:00:31","modified_gmt":"2016-11-30T11:00:31","slug":"tuple","status":"publish","type":"post","link":"http:\/\/uysalyilmaz.com\/index.php\/2016\/11\/30\/tuple\/","title":{"rendered":"Tuple?"},"content":{"rendered":"<p>A tuple is a data structure that has a specific number and sequence of elements.<\/p>\n<p>A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don\u2019t want to create a new class.<\/p>\n<div class=\"code-block code-block-11\">\n<pre class=\"lang:c# decode:true\">System.Tuples<\/pre>\n<p>namespace supports Tuple class in C#.<\/p>\n<\/div>\n<p>The purpose of a tuple is to create a way to return multiple values from a function.<\/p>\n<pre class=\"lang:c# decode:true \">class TupleExample\r\n    {\r\n        static void Main()\r\n        {\r\n            \/\/ Create three-item tuple.\r\n            Tuple&lt;int, string, bool&gt; tuple =\r\n                new Tuple&lt;int, string, bool&gt;(10, \"csharpstar\", true);\r\n            \/\/ Access tuple properties.\r\n            if (tuple.Item1 == 10)\r\n            {\r\n                Console.WriteLine(tuple.Item1);\r\n            }\r\n            if (tuple.Item2 == \"easywcf\")\r\n            {\r\n                Console.WriteLine(tuple.Item2);\r\n            }\r\n            if (tuple.Item3)\r\n            {\r\n                Console.WriteLine(tuple.Item3);\r\n            }\r\n        }\r\n    }<\/pre>\n<p>\u00c7?kt?:<\/p>\n<p>10<br \/>\nTrue<\/p>\n<p>&nbsp;<\/p>\n<h3>Tuple.Create method :<\/h3>\n<pre class=\"lang:default decode:true \">class TupleExample\r\n    {\r\n        static void Main()\r\n        {\r\n            \/\/ Use Tuple.Create static method.\r\n            var tuple = Tuple.Create(\"Csharpstar\", 10, true);\r\n \r\n            \/\/ Test value of string.\r\n            string value = tuple.Item1;\r\n            if (value == \"csharpstar\")\r\n            {\r\n                Console.WriteLine(true);\r\n            }\r\n \r\n            \/\/ Test Item2 and Item3.\r\n            Console.WriteLine(tuple.Item2 == 10);\r\n            Console.WriteLine(!tuple.Item3);\r\n \r\n            \/\/ Write string representation.\r\n            Console.WriteLine(tuple);\r\n        }\r\n    }<\/pre>\n<p>\u00c7?kt?:<\/p>\n<p>true<br \/>\ntrue<br \/>\nfalse<br \/>\n&lt;Csharpstar&#8221;, 10, true&gt;<\/p>\n<h3>Tuple that returns multiple values in C#:<\/h3>\n<pre class=\"lang:default decode:true \">class TupleExample\r\n    {\r\n        static Tuple&lt;string, int&gt; NameAndId()\r\n        {\r\n            \/\/ This method returns multiple values.\r\n            return new Tuple&lt;string, int&gt;(\"Csharpstar\", 10);\r\n        }\r\n \r\n        static void Main(string[] args)\r\n        {\r\n            var result = NameAndId();\r\n            string name = result.Item1;\r\n            int id = result.Item2;\r\n            \/\/ Display the multiple values returned.\r\n            Console.WriteLine(name);\r\n            Console.WriteLine(id);\r\n        }\r\n    }<\/pre>\n<p>\u00c7?kt?:<\/p>\n<div id=\"crayon-583e987ddb0ec941407217-1\" class=\"crayon-line\"><span class=\"crayon-i\">Csharpstar<br \/>\n<\/span><span class=\"crayon-cn\">10<\/span><\/div>\n<div class=\"crayon-line crayon-striped-line\"><\/div>\n<div class=\"crayon-line crayon-striped-line\">\n<h3>When to use Tuples in C#?<\/h3>\n<p>Tuples are commonly used in four ways:<\/p>\n<ul>\n<li>To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.<\/li>\n<li>To provide easy access to, and manipulation of, a data set.<\/li>\n<li>To return multiple values from a method without using out parameters<\/li>\n<li>To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple object as the method argument, you can supply the thread\u2019s startup routine with three items of data.<\/li>\n<\/ul>\n<h3>Issues on Tuple Class:<\/h3>\n<h4>Member Names:<\/h4>\n<p>The main problem with Tuple class is it\u2019s Member Names. It does not have any Semantic meaning.<br \/>\nIf you see a return type of Tuple that doesn\u2019t really tell you anything.<br \/>\nSo it is difficult to understand and maintain the code. It would be far more useful if the return type were something like Tuple<\/p>\n<p><a href=\"http:\/\/www.csharpstar.com\/wp-content\/uploads\/2016\/03\/csharp7_1.jpg\" data-lightbox=\"gal[88]\" rel=\"attachment wp-att-1534\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1534\" src=\"http:\/\/www.csharpstar.com\/wp-content\/uploads\/2016\/03\/csharp7_1.jpg\" alt=\"C#7 Tuples\" width=\"274\" height=\"110\" \/><\/a><\/p>\n<h3><strong><u>So in C#7, Tuples would probably have names, as it makes identifying each member of the Tuple easier.<\/u><br \/>\n<\/strong><\/h3>\n<p>Let\u2019s look at below example to understand the C#7 proposal on Tuples.<\/p>\n<div id=\"crayon-583e987ddb0f0105492206\" class=\"crayon-syntax crayon-theme-vs2012-black crayon-font-courier-new crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\">\n<pre class=\"lang:default decode:true\">public( int sum,int count) Tally(IEnumerable &lt;int&gt; values)\r\n{\r\n\u00a0\u00a0 var s=0; var c=0;\r\n\u00a0\u00a0 foreach (var value in values) { s+= values; c++; }\r\n\u00a0\u00a0 return (s,c);\r\n}<\/pre>\n<p>There are 2 proposals being considered for how tuples would be returned from function:<\/p>\n<\/div>\n<\/div>\n<div id=\"crayon-583e987ddb0f3308073220\" class=\"crayon-syntax crayon-theme-vs2012-black crayon-font-courier-new crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-main\">\n<pre class=\"lang:default decode:true \">\/\/1st proposal\r\nvar t = Tally(myValues);\r\nConsole.WriteLine($\"Sum: {t.sum}, Count: {t.count}\");\r\n\/\/2nd proposal\r\n(var s,var c) = Tally(myValues);\r\nConsole.WriteLine($\"Sum: {s}, Count: {c}\");<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<h3>Tuples and Async :<\/h3>\n<div id=\"crayon-583e987ddb0f7248541584\" class=\"crayon-syntax crayon-theme-vs2012-black crayon-font-courier-new crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-main\">\n<pre class=\"lang:default decode:true \">public async Task&lt;( int sum,int count)&gt; TallyAsync(IAsyncEnumerable &lt;int&gt; values)\r\n{\r\n\u00a0\u00a0 var s=0; var c=0;\r\n\u00a0\u00a0 foreach (await var value in values) { s+= values; c++; }\r\n\u00a0\u00a0 return (s,c);\r\n}\r\n\u00a0\r\nvar t = await Tally(myValues);\r\nConsole.WriteLine($\"Sum: {t.sum}, Count: {t.count}\");\r\n\u00a0\r\n(var s,var c) = await Tally(myValues);\r\nConsole.WriteLine($\"Sum: {s}, Count: {c}\");<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/www.csharpstar.com\/csharp-tuples\/\" target=\"_blank\">Kaynak<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A tuple is a data structure that has a specific number and sequence of elements. A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,34],"tags":[36],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-asp-net-c","category-c-7-0","tag-tuple"],"_links":{"self":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":9,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":97,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/97"}],"wp:attachment":[{"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/uysalyilmaz.com\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}