StringEnumerator.Current プロパティ
コレクション内の現在の要素を取得します。
名前空間: System.Collections.Specialized
アセンブリ: System (system.dll 内)
構文
'宣言
Public ReadOnly Property Current As String
'使用
Dim instance As StringEnumerator
Dim value As String
value = instance.Current
public string Current { get; }
public:
property String^ Current {
String^ get ();
}
/** @property */
public String get_Current ()
public function get Current () : String
プロパティ値
コレクション内の現在の要素。
例外
例外の種類 | 条件 |
---|---|
列挙子が、コレクションの最初の要素の前、または最後の要素の後に位置しています。 |
解説
列挙子を作成した後や Reset を呼び出した後に、コレクションの最初の要素に列挙子を進めるためには、Current の値を読み取る前に MoveNext を呼び出す必要があります。それ以外の場合は、Current は未定義です。
MoveNext への最後の呼び出しで、コレクションの末尾を示す false が返された場合、Current は例外もスローします。
Current は列挙子の位置を移動しません。そのため、Current を連続して呼び出した場合、MoveNext または Reset が呼び出されるまでは同じオブジェクトが返されます。
コレクションが変更されない限り、列挙子は有効なままです。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、次に MoveNext または Reset を呼び出すと InvalidOperationException がスローされます。MoveNext と Current の間でコレクションを変更すると、列挙子が無効になっている場合でも、Current が設定先の要素を返します。
使用例
StringEnumerator のプロパティとメソッドのいくつかの例を次に示します。
Imports System
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 'Main
End Class 'SamplesStringEnumerator
'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.
*/
#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.
*/
import System.* ;
import System.Collections.Specialized.* ;
public class SamplesStringEnumerator
{
public static void main(String[] args)
{
// 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.get_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.get_Current());
}
} //main
} //SamplesStringEnumerator
/*
This code produces the following output.
red
orange
yellow
green
blue
indigo
violet
The first element is red.
*/
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
参照
関連項目
StringEnumerator クラス
StringEnumerator メンバ
System.Collections.Specialized 名前空間
MoveNext
Reset
IEnumerator.Current プロパティ