code_junkie

Will a future version of .NET support tuples in C#?

Nov 11th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. class Program {
  2. static void Main(string[] args) {
  3. Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
  4. PrintStringAndInt(t.Item1, t.Item2);
  5. }
  6. static void PrintStringAndInt(string s, int i) {
  7. Console.WriteLine("{0} {1}", s, i);
  8. }
  9. }
  10.  
  11. var t = new Tuple<string, int>("Hello", 4);
  12.  
  13. var t = Tuple.Create("Hello", 4);
  14.  
  15. #region tuples
  16.  
  17. public class Tuple<T>
  18. {
  19. public Tuple(T first)
  20. {
  21. First = first;
  22. }
  23.  
  24. public T First { get; set; }
  25. }
  26.  
  27. public class Tuple<T, T2> : Tuple<T>
  28. {
  29. public Tuple(T first, T2 second)
  30. : base(first)
  31. {
  32. Second = second;
  33. }
  34.  
  35. public T2 Second { get; set; }
  36. }
  37.  
  38. public class Tuple<T, T2, T3> : Tuple<T, T2>
  39. {
  40. public Tuple(T first, T2 second, T3 third)
  41. : base(first, second)
  42. {
  43. Third = third;
  44. }
  45.  
  46. public T3 Third { get; set; }
  47. }
  48.  
  49. public class Tuple<T, T2, T3, T4> : Tuple<T, T2, T3>
  50. {
  51. public Tuple(T first, T2 second, T3 third, T4 fourth)
  52. : base(first, second, third)
  53. {
  54. Fourth = fourth;
  55. }
  56.  
  57. public T4 Fourth { get; set; }
  58. }
  59.  
  60. #endregion
  61.  
  62. public static class Tuple
  63. {
  64. //Allows Tuple.New(1, "2") instead of new Tuple<int, string>(1, "2")
  65. public static Tuple<T1, T2> New<T1, T2>(T1 t1, T2 t2)
  66. {
  67. return new Tuple<T1, T2>(t1, t2);
  68. }
  69. //etc...
  70. }
  71.  
  72. let (a, b) = someTupleFunc
  73.  
  74. Tuple<int,int> x = someTupleFunc();
  75. int a = x.get_Item1();
  76. int b = x.get_Item2();
  77.  
  78. var p1 = new {a = "A", b = 3};
  79.  
  80. Tuple<Int32, String> t1 = new Tuple<Int32, String>(10, "a");
  81. Tuple<Int32, String, Boolean> t2 = new Tuple<Int32, String, Boolean>(10, "a", true);
  82. if (t1.Equals(t2))
  83. Console.Out.WriteLine(t1 + " == " + t2);
  84. else
  85. Console.Out.WriteLine(t1 + " != " + t2);
  86.  
  87. 10, a != 10, a, True
Add Comment
Please, Sign In to add comment