Advertisement
Caminhoneiro

Tuple

Jul 16th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5.  
  6.  
  7.  
  8. namespace Demos
  9. {
  10.     [TestClass]
  11.     public class TuplesExample
  12.     {
  13.         [TestMethod]
  14.         public void CreatingTuples()
  15.         {
  16.             // Constructor creation
  17.             //Tuples are generic values classes of a lot of typoes
  18.             var tupleOneElement = new Tuple<int>(1); //create a tuple, and creating a int = 1
  19.  
  20.             var tupleTwoElement = new Tuple<int, string>(1, "hello");//Two elements version int = 1, string = "Hello"
  21.  
  22.             var tupleSevenElement =
  23.                 new Tuple<int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7);
  24.            
  25.             var tupleEightElement =
  26.                 new Tuple<int, int, int, int, int, int, int, Tuple<string>>(1, 2, 3, 4, 5, 6, 7,
  27.                                                                                                 new Tuple<string>("hello"));
  28.  
  29.  
  30.            
  31.  
  32.             // Static Create method
  33.  
  34.             var tupleThreeElement = Tuple.Create(42, "hello", DateTime.Now);
  35.         }
  36.        
  37.  
  38.  
  39.  
  40.  
  41.  
  42.         [TestMethod]
  43.         public void AccessingTupleProperties()
  44.         {
  45.             var t = Tuple.Create(42, "hello");
  46.  
  47.             int age = t.Item1;
  48.             string greeting = t.Item2;
  49.  
  50.  
  51.             // t.Item1 = 99;    compiler error, Tuples are immutable
  52.         }
  53.        
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.         [TestMethod]
  67.         public void ComparingTuples()
  68.         {            
  69.             var t1 = Tuple.Create(42, "hello");
  70.             var t2 = Tuple.Create(42, "hello");
  71.  
  72.             // Reference equality
  73.             var isEqualTuples = t1 == t2;
  74.  
  75.             // "value" comparison
  76.             isEqualTuples = t1.Equals(t2);
  77.         }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         [TestMethod]
  89.         public void UsingTuplesToReturnMultipleValues()
  90.         {
  91.             var boxingOpponents = GetOpponentNames();
  92.            
  93.             var opponent1 = boxingOpponents.Item1;
  94.             var opponent2 = boxingOpponents.Item2;
  95.         }
  96.  
  97.         private Tuple<string, string> GetOpponentNames()
  98.         {
  99.             return new Tuple<string, string>("Fred", "Bob");
  100.         }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.         [TestMethod]
  112.         public void UsingTuplesAsCompoundDictionaryKeys()
  113.         {
  114.             var t1 = Tuple.Create(1, "z");
  115.             var t2 = Tuple.Create(2, "a");
  116.             var t3 = Tuple.Create(1, "a");
  117.  
  118.             var d = new SortedDictionary<Tuple<int, string>, string>();
  119.  
  120.             d.Add(t1, "this is Tuple t1");
  121.             d.Add(t2, "this is Tuple t2");
  122.             d.Add(t3, "this is Tuple t3");
  123.  
  124.             foreach (var item in d)
  125.             {
  126.                 Debug.WriteLine(item);
  127.             }
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement