CSharpTest.Net
GetEnumerator Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > LListNode.LList<TNode> Class : GetEnumerator Method

Glossary Item Box

Returns an enumerator that iterates through the collection.

Syntax

Visual Basic (Declaration) 
Public Function GetEnumerator() As IEnumerator(Of TNode)
C# 
public IEnumerator<TNode> GetEnumerator()

Example

Library/Library.Test/TestLinkList.cs

C#Copy Code
LListNode<string>.LList list = new LListNode<string>.LList();
list.AddLast("B");
list.AddFirst("A");
System.Collections.Generic.IEnumerator<LListNode<string>> e = list.GetEnumerator();
using (e)
{
    Assert.IsTrue(e.MoveNext());
    Assert.IsTrue(ReferenceEquals(e.Current, ((System.Collections.IEnumerator)e).Current));
    Assert.AreEqual("A", e.Current.Value);
    list.Remove(e.Current);

    Assert.IsTrue(e.MoveNext());
    Assert.IsTrue(ReferenceEquals(e.Current, ((System.Collections.IEnumerator)e).Current));
    Assert.AreEqual("B", e.Current.Value);
    list.Remove(e.Current);

    Assert.IsFalse(e.MoveNext());

    Assert.IsTrue(list.IsEmpty);
    list.AddLast(String.Empty);

    e.Reset();
    Assert.IsTrue(e.MoveNext());
    Assert.AreEqual(String.Empty, e.Current.Value);
    Assert.IsFalse(e.MoveNext());

    try
    {
        GC.KeepAlive(e.Current);
        Assert.Fail();
    }
    catch (InvalidOperationException)
    { }
}

//now e is disposed...
try
{
    e.MoveNext();
    Assert.Fail();
}
catch (ObjectDisposedException) { }
try
{
    GC.KeepAlive(e.Current);
    Assert.Fail();
}
catch (ObjectDisposedException) { }
try
{
    e.Reset();
    Assert.Fail();
}
catch (ObjectDisposedException) { }
VB.NETCopy Code
Dim list As New LListNode(Of String).LList()
list.AddLast("B")
list.AddFirst("A")
Dim e As System.Collections.Generic.IEnumerator(Of LListNode(Of String)) = list.GetEnumerator()
Using e
    Assert.IsTrue(e.MoveNext())
    Assert.IsTrue(ReferenceEquals(e.Current, (DirectCast(e, System.Collections.IEnumerator)).Current))
    Assert.AreEqual("A", e.Current.Value)
    list.Remove(e.Current)

    Assert.IsTrue(e.MoveNext())
    Assert.IsTrue(ReferenceEquals(e.Current, (DirectCast(e, System.Collections.IEnumerator)).Current))
    Assert.AreEqual("B", e.Current.Value)
    list.Remove(e.Current)

    Assert.IsFalse(e.MoveNext())

    Assert.IsTrue(list.IsEmpty)
    list.AddLast([String].Empty)

    e.Reset()
    Assert.IsTrue(e.MoveNext())
    Assert.AreEqual([String].Empty, e.Current.Value)
    Assert.IsFalse(e.MoveNext())

    Try
        GC.KeepAlive(e.Current)
        Assert.Fail()
    Catch generatedExceptionName As InvalidOperationException
    End Try
End Using

'now e is disposed...
Try
    e.MoveNext()
    Assert.Fail()
Catch generatedExceptionName As ObjectDisposedException
End Try
Try
    GC.KeepAlive(e.Current)
    Assert.Fail()
Catch generatedExceptionName As ObjectDisposedException
End Try
Try
    e.Reset()
    Assert.Fail()
Catch generatedExceptionName As ObjectDisposedException
End Try

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys