This documentation is archived and is not being maintained.

ArraySegment<T> Structure

Delimits a section of a one-dimensional array.

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

[SerializableAttribute]
public struct ArraySegment<T>

Type Parameters

T

The type of the elements in the array segment.

The ArraySegment<T> type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryArraySegment<T>(T[])Initializes a new instance of the ArraySegment<T> structure that delimits all the elements in the specified array.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryArraySegment<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.
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryArrayGets the original array containing the range of elements that the array segment delimits.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCountGets the number of elements in the range delimited by the array segment.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryOffsetGets the position of the first element in the range delimited by the array segment, relative to the start of the original array.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(ArraySegment<T>)Determines whether the specified ArraySegment<T> structure is equal to the current instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current instance. (Overrides ValueType.Equals(Object).)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for the current instance. (Overrides ValueType.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns 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().
Top

  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryEqualityIndicates whether two ArraySegment<T> structures are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInequalityIndicates whether two ArraySegment<T> structures are unequal.
Top

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

*/



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: