Compartir a través de


Tuple<T1, T2>.Item1 Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the value of the current Tuple<T1, T2> object's first component.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public ReadOnly Property Item1 As T1
public T1 Item1 { get; }

Property Value

Type: T1
The value of the current Tuple<T1, T2> object's first component.

Remarks

You can dynamically determine the type of the Item1 component in one of two ways:

  • By calling the GetType method on the value that is returned by the Item1 property.

  • By retrieving the Type object that represents the Tuple<T1, T2> object, and retrieving the first element from the array that is returned by its Type.GetGenericArguments method.

Examples

The example illustrates the use of the Item1 and Item2 properties to define a method that returns multiple values in the form of a 2-tuple.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dividend, divisor As Integer
      Dim result As Tuple(Of Integer, Integer)

      dividend = 136945 : divisor = 178
      result = IntegerDivide(dividend, divisor)
      If result IsNot Nothing Then
         outputBlock.Text += String.Format("{0} \ {1} = {2}, remainder {3}", _
                           dividend, divisor, result.Item1, result.Item2) + vbCrLf
      Else
         outputBlock.Text += String.Format("{0} \ {1} = <Error>", dividend, divisor) & vbCrLf
      End If

      dividend = Int32.MaxValue : divisor = -2073
      result = IntegerDivide(dividend, divisor)
      If result IsNot Nothing Then
         outputBlock.Text += String.Format("{0} \ {1} = {2}, remainder {3}", _
                           dividend, divisor, result.Item1, result.Item2) + vbCrLf
      Else
         outputBlock.Text += String.Format("{0} \ {1} = <Error>", dividend, divisor) & vbCrLf
      End If
   End Sub

   Private Function IntegerDivide(ByVal dividend As Integer, ByVal divisor As Integer) As Tuple(Of Integer, Integer)
      Try
         Dim quotient As Integer = dividend \ divisor
         Dim remainder As Integer = dividend Mod divisor
         Return New Tuple(Of Integer, Integer)(quotient, remainder)
      Catch e As DivideByZeroException
         Return Nothing
      End Try
   End Function
End Module
' The example displays the following output:
'       136945 \ 178 = 769, remainder 63
'       2147483647 \ -2073 = -1035930, remainder 757
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      int dividend, divisor;
      Tuple<int, int> result;

      dividend = 136945; divisor = 178;
      result = IntegerDivide(dividend, divisor);
      if (result != null)
         outputBlock.Text += String.Format(@"{0} \ {1} = {2}, remainder {3}",
                           dividend, divisor, result.Item1, result.Item2) + "\n";
      else
         outputBlock.Text += String.Format(@"{0} \ {1} = <Error>", dividend, divisor) + "\n";

      dividend = Int32.MaxValue; divisor = -2073;
      result = IntegerDivide(dividend, divisor);
      if (result != null)
         outputBlock.Text += String.Format(@"{0} \ {1} = {2}, remainder {3}",
                           dividend, divisor, result.Item1, result.Item2) + "\n";
      else
         outputBlock.Text += String.Format(@"{0} \ {1} = <Error>", dividend, divisor) + "\n";
   }

   private static Tuple<int, int> IntegerDivide(int dividend, int divisor)
   {
      try
      {
         int quotient = dividend / divisor;
         int remainder = dividend % divisor;

         return new Tuple<int, int>(quotient, remainder);
      }
      catch (DivideByZeroException)
      {
         return null;
      }
   }
}
// The example displays the following output:
//       136945 \ 178 = 769, remainder 63
//       2147483647 \ -2073 = -1035930, remainder 757

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.