Parameters
- array
- index
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.NET | Copy 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()) |
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
Reference
ReadOnlyList<T> ClassReadOnlyList<T> Members