Assembly : mscorlib (dans mscorlib.dll)
Syntaxe<SerializableAttribute> _ Public Structure ArraySegment(Of T)
Dim instance As ArraySegment(Of T)
[SerializableAttribute]
public struct ArraySegment<T>
[SerializableAttribute] generic<typename T> public value class ArraySegment
J# prend en charge l'utilisation de types et de méthodes génériques mais pas la déclaration de nouveaux types et de méthodes génériques.
JScript ne prend pas en charge les types et les méthodes génériques.
Paramètres de type
- T
Type des éléments du segment du tableau.
NotesArraySegment est un wrapper autour d'un tableau qui délimite une plage d'éléments dans ce tableau. Plusieurs instances ArraySegment peuvent faire référence au même tableau d'origine et se chevaucher.
La propriété Array retourne le tableau d'origine entier, pas une copie du tableau ; par conséquent, les modifications apportées au tableau retourné par la propriété Array sont apportées au tableau d'origine.
Le tableau d'origine doit être unidimensionnel et comporter un index de base zéro.
ExempleL'exemple de code suivant passe une structure ArraySegment à une méthode.
Imports System Public Class SamplesArray Public Shared Sub Main() ' Create and initialize a new string array. Dim myArr As String() = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"} ' Display the initial contents of the array. Console.WriteLine("The original array initially contains:") PrintIndexAndValues(myArr) ' Define an array segment that contains the entire array. Dim myArrSegAll As New ArraySegment(Of String)(myArr) ' Display the contents of the ArraySegment. Console.WriteLine("The first array segment (with all the array's elements) contains:") PrintIndexAndValues(myArrSegAll) ' Define an array segment that contains the middle five values of the array. Dim myArrSegMid As New ArraySegment(Of String)(myArr, 2, 5) ' Display the contents of the ArraySegment. Console.WriteLine("The second array segment (with the middle five elements) contains:") PrintIndexAndValues(myArrSegMid) ' Modify the fourth element of the first array segment myArrSegAll. myArrSegAll.Array(3) = "LION" ' Display the contents of the second array segment myArrSegMid. ' Note that the value of its second element also changed. Console.WriteLine("After the first array segment is modified, the second array segment now contains:") PrintIndexAndValues(myArrSegMid) End Sub 'Main Public Shared Sub PrintIndexAndValues(arrSeg As ArraySegment(Of String)) Dim i As Integer For i = arrSeg.Offset To (arrSeg.Offset + arrSeg.Count - 1) Console.WriteLine(" [{0}] : {1}", i, arrSeg.Array(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues Public Shared Sub PrintIndexAndValues(myArr as String()) Dim i As Integer For i = 0 To (myArr.Length - 1) Console.WriteLine(" [{0}] : {1}", i, myArr(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues End Class 'SamplesArray 'This code produces the following output. ' 'The original array initially contains: ' [0] : The ' [1] : quick ' [2] : brown ' [3] : fox ' [4] : jumps ' [5] : over ' [6] : the ' [7] : lazy ' [8] : dog ' 'The first array segment (with all the array's elements) contains: ' [0] : The ' [1] : quick ' [2] : brown ' [3] : fox ' [4] : jumps ' [5] : over ' [6] : the ' [7] : lazy ' [8] : dog ' 'The second array segment (with the middle five elements) contains: ' [2] : brown ' [3] : fox ' [4] : jumps ' [5] : over ' [6] : the ' 'After the first array segment is modified, the second array segment now contains: ' [2] : brown ' [3] : LION ' [4] : jumps ' [5] : over ' [6] : the
using System; public class SamplesArray { public static void Main() { // Create and initialize a new string array. String[] myArr = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" }; // Display the initial contents of the array. Console.WriteLine( "The original array initially contains:" ); PrintIndexAndValues( myArr ); // Define an array segment that contains the entire array. ArraySegment<String> myArrSegAll = new ArraySegment<String>( myArr ); // Display the contents of the ArraySegment. Console.WriteLine( "The first array segment (with all the array's elements) contains:" ); PrintIndexAndValues( myArrSegAll ); // Define an array segment that contains the middle five values of the array. ArraySegment<String> myArrSegMid = new ArraySegment<String>( myArr, 2, 5 ); // Display the contents of the ArraySegment. Console.WriteLine( "The second array segment (with the middle five elements) contains:" ); PrintIndexAndValues( myArrSegMid ); // Modify the fourth element of the first array segment myArrSegAll. myArrSegAll.Array[3] = "LION"; // Display the contents of the second array segment myArrSegMid. // Note that the value of its second element also changed. Console.WriteLine( "After the first array segment is modified, the second array segment now contains:" ); PrintIndexAndValues( myArrSegMid ); } public static void PrintIndexAndValues( ArraySegment<String> arrSeg ) { for ( int i = arrSeg.Offset; i < (arrSeg.Offset + arrSeg.Count); i++ ) { Console.WriteLine( " [{0}] : {1}", i, arrSeg.Array[i] ); } Console.WriteLine(); } public static void PrintIndexAndValues( String[] myArr ) { for ( int i = 0; i < myArr.Length; i++ ) { Console.WriteLine( " [{0}] : {1}", i, myArr[i] ); } Console.WriteLine(); } } /* This code produces the following output. The original array initially contains: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog The first array segment (with all the array's elements) contains: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog The second array segment (with the middle five elements) contains: [2] : brown [3] : fox [4] : jumps [5] : over [6] : the After the first array segment is modified, the second array segment now contains: [2] : brown [3] : LION [4] : jumps [5] : over [6] : the */
using namespace System; namespace Sample { public ref class SampleArray { public: static void Work() { // Create and initialize a new string array. array <String^>^ words = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; // Display the initial contents of the array. Console::WriteLine("The first array segment" " (with all the array's elements) contains:"); PrintIndexAndValues(words); // Define an array segment that contains the entire array. ArraySegment<String^> segment(words); // Display the contents of the ArraySegment. Console::WriteLine("The first array segment" " (with all the array's elements) contains:"); PrintIndexAndValues(segment); // Define an array segment that contains the middle five // values of the array. ArraySegment<String^> middle(words, 2, 5); // Display the contents of the ArraySegment. Console::WriteLine("The second array segment" " (with the middle five elements) contains:"); PrintIndexAndValues(middle); // Modify the fourth element of the first array // segment segment.Array[3] = "LION"; // Display the contents of the second array segment // middle. Note that the value of its second element // also changed. Console::WriteLine("After the first array segment" " is modified,the second array segment" " now contains:"); PrintIndexAndValues(middle); Console::ReadLine(); } static void PrintIndexAndValues(ArraySegment<String^>^ segment) { for (int i = segment->Offset; i < (segment->Offset + segment->Count); i++) { Console::WriteLine(" [{0}] : {1}", i, segment->Array[i]); } Console::WriteLine(); } static void PrintIndexAndValues(array<String^>^ words) { for (int i = 0; i < words->Length; i++) { Console::WriteLine(" [{0}] : {1}", i, words[i]); } Console::WriteLine(); } }; } int main() { Sample::SampleArray::Work(); return 0; } /* This code produces the following output. The original array initially contains: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog The first array segment (with all the array's elements) contains: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog The second array segment (with the middle five elements) contains: [2] : brown [3] : fox [4] : jumps [5] : over [6] : the After the first array segment is modified, the second array segment now contains: [2] : brown [3] : LION [4] : jumps [5] : over [6] : the */
Sécurité des threads
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ