Getting the Type of the Tuple’s Element in C#

CsharpServer Side ProgrammingProgramming

<p style="">To get the type of the Tuple&rsquo;s element, the code is as follows &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/I1UrwSQG" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; public class Demo { &nbsp; &nbsp;public static void Main(String[] args){ &nbsp; &nbsp; &nbsp; var tuple1 = Tuple.Create(150, 1500, Tuple.Create(50, 100)); &nbsp; &nbsp; &nbsp; var tuple2 = Tuple.Create(150, 1500, Tuple.Create(100, 200)); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Is Tuple1 equal to Tuple2? = &quot;+tuple1.Equals(tuple2)); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;HashCode of Tuple1 = &quot;+tuple1.GetHashCode()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Type of Tuple1 = &quot;+tuple1.GetType()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;HashCode of Tuple2 = &quot;+tuple2.GetHashCode()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Type of Tuple1 = &quot;+tuple2.GetType()); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Is Tuple1 equal to Tuple2? = False HashCode of Tuple1 = 188892 Type of Tuple1 = System.Tuple`3[System.Int32,System.Int32,System.Tuple`2[System.Int32,System.Int32]] HashCode of Tuple2 = 191462 Type of Tuple1 = System.Tuple`3[System.Int32,System.Int32,System.Tuple`2[System.Int32,System.Int32]]</pre><h2>Example</h2><p>Let us now see another example &minus;</p><p><a class="demo" href="http://tpcg.io/yQrp4Mzj" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; public class Demo { &nbsp; &nbsp;public static void Main(String[] args){ &nbsp; &nbsp; &nbsp; var tuple1 = Tuple.Create(10, 20); &nbsp; &nbsp; &nbsp; var tuple2 = Tuple.Create(30, 40); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Is Tuple1 equal to Tuple2? = &quot;+tuple1.Equals(tuple2)); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;HashCode of Tuple1 = &quot;+tuple1.GetHashCode()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Type of Tuple1 = &quot;+tuple1.GetType()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;HashCode of Tuple2 = &quot;+tuple2.GetHashCode()); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Type of Tuple1 = &quot;+tuple2.GetType()); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Is Tuple1 equal to Tuple2? = False HashCode of Tuple1 = 350 Type of Tuple1 = System.Tuple`2[System.Int32,System.Int32] HashCode of Tuple2 = 1014 Type of Tuple1 = System.Tuple`2[System.Int32,System.Int32]</pre>
raja
Updated on 06-Dec-2019 07:48:05

Advertisements