CSharpTest.Net
IndexOf Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > ReadOnlyList<T> Class : IndexOf Method

item

Glossary Item Box

Returns the zero-based index of the item or -1 if not found.

Syntax

Visual Basic (Declaration) 
Public Function IndexOf( _
   ByVal item As T _
) As Integer
C# 
public int IndexOf( 
   T item
)

Parameters

item

Example

Library/Library.Test/TestReadOnlyList.cs

C#Copy Code
List<string> strings = new List<string>(new string[] { "a", "b", "c" });
ReadOnlyList<String> read = new ReadOnlyList<string>(strings);
strings.Add("d");
Assert.AreEqual(3, read.Count);
Assert.IsTrue(read.Contains("a"));
Assert.AreEqual(0, read.IndexOf("a"));
Assert.IsTrue(read.Contains("b"));
Assert.AreEqual(1, read.IndexOf("b"));
Assert.IsTrue(read.Contains("c"));
Assert.AreEqual(2, read.IndexOf("c"));
Assert.IsFalse(read.Contains("d"));
Assert.AreEqual(-1, read.IndexOf("d"));
Assert.AreEqual("a,b,c", String.Join(",", read.ToArray()));
Assert.AreEqual("a,b,c", String.Join(",", new List<String>(read).ToArray()));
            
string[] arcopy = new string[3];
read.CopyTo(arcopy, 0);
Assert.AreEqual("a,b,c", String.Join(",", arcopy));

System.Collections.IEnumerator en = ((System.Collections.IEnumerable)read).GetEnumerator();
Assert.IsTrue(en.MoveNext());
Assert.AreEqual("a", en.Current);
Assert.IsTrue(en.MoveNext());
Assert.AreEqual("b", en.Current);
Assert.IsTrue(en.MoveNext());
Assert.AreEqual("c", en.Current);
Assert.IsFalse(en.MoveNext());
VB.NETCopy Code
Dim strings As New List(Of String)(New String() {"a", "b", "c"})
Dim read As ReadOnlyList(Of [String]) = New ReadOnlyList(Of String)(strings)
strings.Add("d")
Assert.AreEqual(3, read.Count)
Assert.IsTrue(read.Contains("a"))
Assert.AreEqual(0, read.IndexOf("a"))
Assert.IsTrue(read.Contains("b"))
Assert.AreEqual(1, read.IndexOf("b"))
Assert.IsTrue(read.Contains("c"))
Assert.AreEqual(2, read.IndexOf("c"))
Assert.IsFalse(read.Contains("d"))
Assert.AreEqual(-1, read.IndexOf("d"))
Assert.AreEqual("a,b,c", [String].Join(",", read.ToArray()))
Assert.AreEqual("a,b,c", [String].Join(",", New List(Of [String])(read).ToArray()))

Dim arcopy As String() = New String(3) {}
read.CopyTo(arcopy, 0)
Assert.AreEqual("a,b,c", [String].Join(",", arcopy))

Dim en As System.Collections.IEnumerator = (DirectCast(read, System.Collections.IEnumerable)).GetEnumerator()
Assert.IsTrue(en.MoveNext())
Assert.AreEqual("a", en.Current)
Assert.IsTrue(en.MoveNext())
Assert.AreEqual("b", en.Current)
Assert.IsTrue(en.MoveNext())
Assert.AreEqual("c", en.Current)
Assert.IsFalse(en.MoveNext())

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys