ArraySegment<T> 构造函数 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 ArraySegment<T> 结构的新实例。
重载
| ArraySegment<T>(T[]) | 初始化 ArraySegment<T> 结构的新实例,该结构用于分隔指定数组中的所有元素。 | 
| ArraySegment<T>(T[], Int32, Int32) | 初始化 ArraySegment<T> 结构的新结构,该结构用于分隔指定数组中指定的元素范围。 | 
示例
下面的代码示例将结构 ArraySegment<T> 传递给方法。
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
    */
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
*/
open System
// Print functions.
let printIndexAndValues (myArr: string []) =
    for i = 0 to myArr.Length - 1 do
        printfn $"   [{i}] : {myArr[i]}"
    printfn ""
let printIndexAndValuesSeg (arrSeg: ArraySegment<string>) =
    for i = arrSeg.Offset to arrSeg.Offset + arrSeg.Count - 1 do
        printfn $"   [{i}] : {arrSeg.Array[i]}"
    printfn ""
// Create and initialize a new string array.
let myArr = [| "The"; "quick"; "brown"; "fox"; "jumps"; "over"; "the"; "lazy"; "dog" |]
// Display the initial contents of the array.
printfn "The original array initially contains:"
printIndexAndValues myArr
// Define an array segment that contains the entire array.
let myArrSegAll = ArraySegment<string>(myArr)
// Display the contents of the ArraySegment.
printfn "The first array segment (with all the array's elements) contains:"
printIndexAndValuesSeg myArrSegAll
// Define an array segment that contains the middle five values of the array.
let myArrSegMid = ArraySegment<string>(myArr, 2, 5)
// Display the contents of the ArraySegment.
printfn "The second array segment (with the middle five elements) contains:"
printIndexAndValuesSeg 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.
printfn "After the first array segment is modified, the second array segment now contains:"
printIndexAndValuesSeg myArrSegMid
(*
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
*)
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
    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
    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
End Class
'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
ArraySegment<T>(T[])
初始化 ArraySegment<T> 结构的新实例,该结构用于分隔指定数组中的所有元素。
public:
 ArraySegment(cli::array <T> ^ array);public ArraySegment (T[] array);new ArraySegment<'T> : 'T[] -> ArraySegment<'T>Public Sub New (array As T())参数
- array
- T[]
要包装的数组。
例外
array 为 null。
注解
此构造函数创建一个 ArraySegment<T> 用于分隔所有元素的 array构造函数。 也就是说,属性 Offset 为 ArraySegment<T> 0,其 Count 属性是长度 array。 若要创建仅分隔数组的一部分的函数 ArraySegment<T> ,请使用 ArraySegment<T>(T[], Int32, Int32) 构造函数。
原始数组必须是一维数组,并且必须具有从零开始的索引。
多个 ArraySegment<T> 实例可以引用相同的原始数组,并且可以重叠。
另请参阅
适用于
ArraySegment<T>(T[], Int32, Int32)
初始化 ArraySegment<T> 结构的新结构,该结构用于分隔指定数组中指定的元素范围。
public:
 ArraySegment(cli::array <T> ^ array, int offset, int count);public ArraySegment (T[] array, int offset, int count);new ArraySegment<'T> : 'T[] * int * int -> ArraySegment<'T>Public Sub New (array As T(), offset As Integer, count As Integer)参数
- array
- T[]
包含要分隔的元素范围的数组。
- offset
- Int32
范围中第一个元素的从零开始的索引。
- count
- Int32
范围中的元素数。
例外
array 上声明的默认值为 null。
offset 或 count 小于 0。
offset 和 count 未在 array 中指定有效范围。
注解
原始数组必须是一维数组,并且必须具有从零开始的索引。
多个 ArraySegment<T> 实例可以引用相同的原始数组,并且可以重叠。