Use Tuple to get more than one value from a method : Tuple « Data Types « C# / C Sharp






Use Tuple to get more than one value from a method

 

using System;

public class Class1
{
   public static void Main()
   {
      int dividend, divisor;
      Tuple<int, int> result;

      dividend = 136945; 
      divisor = 178;
      result = IntegerDivide(dividend, divisor);
      if (result != null)
         Console.WriteLine(@"{0} \ {1} = {2}, remainder {3}", dividend, divisor, result.Item1, result.Item2);
      else
         Console.WriteLine(@"{0} \ {1} = <Error>", dividend, divisor);
   }

   private static Tuple<int, int> IntegerDivide(int dividend, int divisor)
   {
      try {
         int remainder;
         int quotient = Math.DivRem(dividend, divisor, out remainder);
         return new Tuple<int, int>(quotient, remainder);
      }   
      catch (DivideByZeroException) {
         return null;
      }      
   }
}

   
  








Related examples in the same category

1.Tuple Class Provides static methods for creating tuple objects.
2.Tuple(T1, T2) Represents a 2-tuple, or pair.