Share via


StringEnumerator.Current 屬性

定義

取得集合中目前的項目。

public:
 property System::String ^ Current { System::String ^ get(); };
public string Current { get; }
public string? Current { get; }
member this.Current : string
Public ReadOnly Property Current As String

屬性值

集合中的目前元素。

例外狀況

列舉值是放在集合的第一個項目之前,或最後一個項目之後。

範例

下列程式代碼範例示範 的數個屬性和方法 StringEnumerator

#using <System.dll>

using namespace System;
using namespace System::Collections::Specialized;
int main()
{
   
   // Creates and initializes a StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"red","orange","yellow","green","blue","indigo","violet"};
   myCol->AddRange( myArr );
   
   // Enumerates the elements in the StringCollection.
   StringEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "{0}", myEnumerator->Current );

   Console::WriteLine();
   
   // Resets the enumerator and displays the first element again.
   myEnumerator->Reset();
   if ( myEnumerator->MoveNext() )
      Console::WriteLine( "The first element is {0}.", myEnumerator->Current );
}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/
using System;
using System.Collections.Specialized;

public class SamplesStringEnumerator  {

   public static void Main()  {

      // Creates and initializes a StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
      myCol.AddRange( myArr );

      // Enumerates the elements in the StringCollection.
      StringEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "{0}", myEnumerator.Current );
      Console.WriteLine();

      // Resets the enumerator and displays the first element again.
      myEnumerator.Reset();
      if ( myEnumerator.MoveNext() )
         Console.WriteLine( "The first element is {0}.", myEnumerator.Current );
   }
}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/
Imports System.Collections.Specialized

Public Class SamplesStringEnumerator

   Public Shared Sub Main()

      ' Creates and initializes a StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
      myCol.AddRange(myArr)

      ' Enumerates the elements in the StringCollection.
      Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine("{0}", myEnumerator.Current)
      End While
      Console.WriteLine()

      ' Resets the enumerator and displays the first element again.
      myEnumerator.Reset()
      If myEnumerator.MoveNext() Then
         Console.WriteLine("The first element is {0}.", myEnumerator.Current)
      End If 

   End Sub

End Class


'This code produces the following output.
'
'red
'orange
'yellow
'green
'blue
'indigo
'violet
'
'The first element is red.

備註

建立列舉值或呼叫 之後 Reset ,必須呼叫 , MoveNext 才能將列舉值前進至集合的第一個專案,再讀取的值 Current;否則 Current 為未定義。

Current 如果最後一次呼叫 MoveNextfalse回 ,則也會擲回例外狀況,這表示集合的結尾。

Current 不會移動列舉值的位置,而且連續呼叫會 Current 傳回相同的物件,直到 MoveNext 呼叫 或 Reset 為止。

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如新增、修改或刪除專案,列舉值會無法復原,而下一次InvalidOperationException呼叫 MoveNextReset 會擲回 。 如果在和 Current之間MoveNext修改集合,Current則會傳回它設定為 的專案,即使列舉值已經失效也一樣。

適用於

另請參閱