public override void CopyTo(Array array, int index) { lock (_root) { _list.CopyTo(array, index); } }
// ICollection public override void CopyTo(Array array, int arrayIndex) { lock (host.SyncRoot) { host.CopyTo(array, arrayIndex); } }
static public int CopyTo(IntPtr l) { try { System.Collections.SortedList self = (System.Collections.SortedList)checkSelf(l); System.Array a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.CopyTo(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
public void TestCopyTo () { SortedList sl1 = new SortedList (); for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); } { try { sl1.CopyTo (null, 2); Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null"); } catch (ArgumentNullException) { } } { try { Char [,] c2 = new Char [2, 2]; sl1.CopyTo (c2, 2); Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray"); } catch (ArgumentException) { } } { try { Char [] c1 = new Char [2]; sl1.CopyTo (c1, -2); Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative"); } catch (ArgumentOutOfRangeException) { } } { try { Char [] c1 = new Char [2]; sl1.CopyTo (c1, 3); Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large"); } catch (ArgumentException) { } } { try { Char [] c1 = new Char [2]; sl1.CopyTo (c1, 1); Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array"); } catch (ArgumentException) { } } { try { Char [] c2 = new Char [15]; sl1.CopyTo (c2, 0); Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types"); } catch (InvalidCastException) { } } // CopyTo function does not work well with SortedList // even example at MSDN gave InvalidCastException // thus, it is NOT tested here /* sl1.Clear(); for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);} Char[] copy = new Char[15]; Array.Clear(copy,0,copy.Length); copy.SetValue( "The", 0 ); copy.SetValue( "quick", 1 ); copy.SetValue( "brown", 2 ); copy.SetValue( "fox", 3 ); copy.SetValue( "jumped", 4 ); copy.SetValue( "over", 5 ); copy.SetValue( "the", 6 ); copy.SetValue( "lazy", 7 ); copy.SetValue( "dog", 8 ); sl1.CopyTo(copy,1); AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0)); AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1)); for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i)); AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8)); */ }
void ICollection.CopyTo(Array dest, int index) { list.CopyTo(dest, index); }
private void DoICollectionTests(SortedList good, ICollection bad, Hashtable hsh1, DicType dic) { if (good.Count != bad.Count) hsh1["Count"] = ""; if (good.IsSynchronized != bad.IsSynchronized) hsh1["IsSynchronized"] = ""; if (good.SyncRoot != bad.SyncRoot) hsh1["SyncRoot"] = ""; //CopyTo string[] iArr1 = null; string[] iArr2 = null; DictionaryEntry[] dicEnt1; //CopyTo() copies the values!!! iArr1 = new string[good.Count]; iArr2 = new string[good.Count]; bad.CopyTo(iArr2, 0); if (dic == DicType.Value) { dicEnt1 = new DictionaryEntry[good.Count]; good.CopyTo(dicEnt1, 0); for (int i = 0; i < good.Count; i++) iArr1[i] = (string)((DictionaryEntry)dicEnt1[i]).Value; for (int i = 0; i < iArr1.Length; i++) { if (!iArr1[i].Equals(iArr2[i])) hsh1["CopyTo"] = "vanila"; } iArr1 = new string[good.Count + 5]; iArr2 = new string[good.Count + 5]; for (int i = 0; i < good.Count; i++) iArr1[i + 5] = (string)((DictionaryEntry)dicEnt1[i]).Value; bad.CopyTo(iArr2, 5); for (int i = 5; i < iArr1.Length; i++) { if (!iArr1[i].Equals(iArr2[i])) { hsh1["CopyTo"] = "5"; } } } else if (dic == DicType.Key) { for (int i = 0; i < iArr1.Length; i++) { if (!good.Contains(iArr2[i])) hsh1["CopyTo"] = "Key"; } } try { bad.CopyTo(iArr2, -1); hsh1["CopyTo"] = ""; } catch (ArgumentException) { } catch (Exception ex) { hsh1["CopyTo"] = ex; } try { bad.CopyTo(null, 5); hsh1["CopyTo"] = ""; } catch (ArgumentNullException) { } catch (Exception ex) { hsh1["CopyTo"] = ex; } //Enumerator IEnumerator ienm1; IEnumerator ienm2; ienm1 = good.GetEnumerator(); ienm2 = bad.GetEnumerator(); DoTheEnumerator(ienm1, ienm2, hsh1, dic, good); }