本文整理汇总了C#中Bag.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# Bag.CopyTo方法的具体用法?C# Bag.CopyTo怎么用?C# Bag.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bag
的用法示例。
在下文中一共展示了Bag.CopyTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestNewBag
public void TestNewBag()
{
Bag<string> b = new Bag<string>();
Assert.AreEqual(0, b.Count);
Assert.IsFalse(b.IsReadOnly);
Assert.AreEqual(0, b.Count);
Assert.IsFalse(b.Remove("foo"));
Assert.IsFalse(b.Contains("bar"));
try
{
b.CopyTo(new string[0], 0);
}
catch (Exception)
{
Assert.Fail("CopyTo() shouldn't touch parameter array if nothing to copy");
}
}
示例2: TestCopyTo
public void TestCopyTo()
{
Bag<string> b = new Bag<string>();
try { b.CopyTo(new string[0], 0); }
catch (Exception) { Assert.Fail("shouldn't access array unnecessarily"); }
b.Add("foo");
b.Add("foo");
Assert.AreEqual(2, b.Count);
try
{
b.CopyTo(new string[1], 0);
Assert.Fail("shouldn't go over array length");
}
catch (ArgumentException) { /* expected result */ }
string[] values = new string[2];
try
{
b.CopyTo(values, 0);
Assert.AreEqual("foo", values[0]);
Assert.AreEqual("foo", values[1]);
}
catch (ArgumentException)
{
Assert.Fail("shouldn't go over array length");
}
values = new string[] { "", "", "" };
try
{
b.CopyTo(values, 0);
Assert.AreEqual("foo", values[0]);
Assert.AreEqual("foo", values[1]);
Assert.AreEqual("", values[2]);
}
catch (ArgumentException)
{
Assert.Fail("shouldn't go over array length");
}
}
示例3: TestCopyToOOBI
public void TestCopyToOOBI()
{
Bag<string> b = new Bag<string>();
b.Add("foo");
try { b.CopyTo(new string[0], 0); }
catch (ArgumentException) { return; }
Assert.Fail();
}
示例4: CopyToExample
public void CopyToExample()
{
var bag = new Bag<string> {"cat", "dog", "canary", "canary"};
// Create new string array - the count is 4 because "canary" will exists twice.
var stringArray = new string[4];
bag.CopyTo(stringArray, 0);
}