Tuple<T1,T2>.Item2 Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the value of the current Tuple<T1,T2> object's second component.
public:
property T2 Item2 { T2 get(); };
public T2 Item2 { get; }
member this.Item2 : 'T2
Public ReadOnly Property Item2 As T2
The value of the current Tuple<T1,T2> object's second component.
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.
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);
dividend = Int32.MaxValue; divisor = -2073;
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;
}
}
}
// The example displays the following output:
// 136945 \ 178 = 769, remainder 63
// 2147483647 \ -2073 = -1035930, remainder 757
open System
let integerDivide (dividend: int) divisor =
try
let quotient, remainder = Math.DivRem(dividend, divisor)
Tuple<int, int>(quotient, remainder)
with :? DivideByZeroException ->
Unchecked.defaultof<Tuple<int, int>>
[<EntryPoint>]
let main _ =
let dividend = 136945
let divisor = 178
let result = integerDivide dividend divisor
if box result <> null then
printfn $@"{dividend} \ {divisor} = {result.Item1}, remainder {result.Item2}"
else
printfn $@"{dividend} \ {divisor} = <Error>"
let dividend = Int32.MaxValue
let divisor = -2073
let result = integerDivide dividend divisor
if box result <> null then
printfn $@"{dividend} \ {divisor} = {result.Item1}, remainder {result.Item2}"
else
printfn $@"{dividend} \ {divisor} = <Error>"
0
// The example displays the following output:
// 136945 \ 178 = 769, remainder 63
// 2147483647 \ -2073 = -1035930, remainder 757
Module modMain
Public Sub Main()
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
Console.WriteLine("{0} \ {1} = {2}, remainder {3}",
dividend, divisor, result.Item1, result.Item2)
Else
Console.WriteLine("{0} \ {1} = <Error>", dividend, divisor)
End If
dividend = Int32.MaxValue : divisor = -2073
result = IntegerDivide(dividend, divisor)
If result IsNot Nothing Then
Console.WriteLine("{0} \ {1} = {2}, remainder {3}",
dividend, divisor, result.Item1, result.Item2)
Else
Console.WriteLine("{0} \ {1} = <Error>", dividend, divisor)
End If
End Sub
Private Function IntegerDivide(dividend As Integer, divisor As Integer) As Tuple(Of Integer, Integer)
Try
Dim remainder As Integer
Dim quotient As Integer = Math.DivRem(dividend, divisor, remainder)
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
You can dynamically determine the type of the Item2 component in one of two ways:
By calling the
GetType
method on the value that is returned by the Item2 property.By retrieving the Type object that represents the Tuple<T1,T2> object, and retrieving the second element from the array that is returned by its Type.GetGenericArguments method.
Produit | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :