Export (0) Print
Expand All

ArraySegment<T>::Count Property

Gets the number of elements in the range delimited by the array segment.

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

public:
virtual property int Count {
	int get () sealed;
}

Property Value

Type: System::Int32
The number of elements in the range delimited by the ArraySegment<T>.

Implements

ICollection<T>::Count
IReadOnlyCollection<T>::Count

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

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

    */

.NET Framework

Supported in: 4.5, 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

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft