ArraySegment<T> Constructors

Definition

Initializes a new instance of the ArraySegment<T> structure.

Overloads

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.

Examples

The following code example passes an ArraySegment<T> structure to a method.

C#
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

*/

ArraySegment<T>(T[])

Source:
ArraySegment.cs
Source:
ArraySegment.cs
Source:
ArraySegment.cs

Initializes a new instance of the ArraySegment<T> structure that delimits all the elements in the specified array.

C#
public ArraySegment(T[] array);

Parameters

array
T[]

The array to wrap.

Exceptions

array is null.

Remarks

This constructor creates an ArraySegment<T> that delimits all the elements of array. That is, the Offset property of the ArraySegment<T> is 0 and its Count property is the length of array. To create an ArraySegment<T> that delimits only part of an array, use the ArraySegment<T>(T[], Int32, Int32) constructor.

The original array must be one-dimensional and must have zero-based indexing.

Multiple ArraySegment<T> instances can refer to the same original array and can overlap.

See also

Applies to

.NET 10 and other versions
Product 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, 10
.NET Framework 2.0, 3.0, 3.5, 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

ArraySegment<T>(T[], Int32, Int32)

Source:
ArraySegment.cs
Source:
ArraySegment.cs
Source:
ArraySegment.cs

Initializes a new instance of the ArraySegment<T> structure that delimits the specified range of the elements in the specified array.

C#
public ArraySegment(T[] array, int offset, int count);

Parameters

array
T[]

The array containing the range of elements to delimit.

offset
Int32

The zero-based index of the first element in the range.

count
Int32

The number of elements in the range.

Exceptions

array is null.

offset or count is less than 0.

offset and count do not specify a valid range in array.

Remarks

The original array must be one-dimensional and must have zero-based indexing.

Multiple ArraySegment<T> instances can refer to the same original array and can overlap.

See also

Applies to

.NET 10 and other versions
Product 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, 10
.NET Framework 2.0, 3.0, 3.5, 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