ArraySegment<T> Structure
Delimits a section of a one-dimensional array.
Assembly: mscorlib (in mscorlib.dll)
The ArraySegment<T> type exposes the following members.
Name | Description | |
---|---|---|
ArraySegment<T>(T[]) | Initializes a new instance of the ArraySegment<T> structure that delimits all the elements in the specified array. | |
ArraySegment<T>(T[], Int32, Int32) | Initializes a new instance of the ArraySegment<T> structure that delimits the specified range of the elements in the specified array. |
Name | Description | |
---|---|---|
Array | Gets the original array containing the range of elements that the array segment delimits. | |
Count | Gets the number of elements in the range delimited by the array segment. | |
Offset | Gets the position of the first element in the range delimited by the array segment, relative to the start of the original array. |
Name | Description | |
---|---|---|
Equals(ArraySegment<T>) | Determines whether the specified ArraySegment<T> structure is equal to the current instance. | |
Equals(Object) | Determines whether the specified object is equal to the current instance. (Overrides ValueType.Equals(Object).) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Returns the hash code for the current instance. (Overrides ValueType.GetHashCode().) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns the fully qualified type name of this instance. (Inherited from ValueType.) In XNA Framework 3.0, this member is inherited from Object.ToString(). In Portable Class Library Portable Class Library, this member is inherited from Object.ToString(). |
Name | Description | |
---|---|---|
Equality | Indicates whether two ArraySegment<T> structures are equal. | |
Inequality | Indicates whether two ArraySegment<T> structures are unequal. |
ArraySegment<T> is a wrapper around an array that delimits a range of elements in that array. Multiple ArraySegment<T> instances can refer to the same original array and can overlap.
The Array property returns the entire original array, not a copy of the array; therefore, changes made to the array returned by the Array property are made to the original array.
The original array must be one-dimensional and must have zero-based indexing.
The following code example passes an ArraySegment<T> structure to a method.
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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.