本文整理汇总了C#中ConcurrentQueue.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentQueue.CopyTo方法的具体用法?C# ConcurrentQueue.CopyTo怎么用?C# ConcurrentQueue.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentQueue
的用法示例。
在下文中一共展示了ConcurrentQueue.CopyTo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test7_Exceptions
public static void Test7_Exceptions()
{
ConcurrentQueue<int> queue = null;
Assert.Throws<ArgumentNullException>(
() => queue = new ConcurrentQueue<int>((IEnumerable<int>)null));
// "Test7_Exceptions: The constructor didn't throw ANE when null collection passed");
queue = new ConcurrentQueue<int>();
//CopyTo
Assert.Throws<ArgumentNullException>( () => queue.CopyTo(null, 0));
// "Test7_Exceptions: CopyTo didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>( () => queue.CopyTo(new int[1], -1));
// "Test7_Exceptions: CopyTo didn't throw AORE when negative array index passed");
}
示例2: RunConcurrentQueueTest5_CtorAndCopyToAndToArray
// Instantiates the queue w/ the enumerator ctor and validates the resulting copyto & toarray.
private static bool RunConcurrentQueueTest5_CtorAndCopyToAndToArray(int count)
{
TestHarness.TestLog("* RunConcurrentQueueTest5_CtorAndCopyToAndToArray()");
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentQueue<int> s = new ConcurrentQueue<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
if (sa1.Length != arr.Length)
{
TestHarness.TestLog(" > ToArray resulting array is diff length: got {0}, wanted {1}",
sa1.Length, arr.Length);
return false;
}
for (int i = 0; i < sa1.Length; i++)
{
if (sa1[i] != arr[i])
{
TestHarness.TestLog(" > ToArray returned an array w/ diff contents: got {0}, wanted {1}",
sa1[i], arr[i]);
return false;
}
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
if (sa2.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(int[]) resulting array is diff length: got {0}, wanted {1}",
sa2.Length, arr.Length);
return false;
}
for (int i = 0; i < sa2.Length; i++)
{
if (sa2[i] != arr[i])
{
TestHarness.TestLog(" > CopyTo(int[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa2[i], arr[i]);
return false;
}
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
if (sa3.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(object[]) resulting array is diff length: got {0}, wanted {1}",
sa3.Length, arr.Length);
return false;
}
for (int i = 0; i < sa3.Length; i++)
{
if ((int)sa3[i] != arr[i])
{
TestHarness.TestLog(" > CopyTo(object[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa3[i], arr[i]);
return false;
}
}
return true;
}
示例3: CopyTo_AllItemsCopiedAtCorrectLocation
public void CopyTo_AllItemsCopiedAtCorrectLocation(int count, bool viaInterface)
{
var q = new ConcurrentQueue<int>(Enumerable.Range(0, count));
var c = (ICollection)q;
int[] arr = new int[count];
if (viaInterface)
{
c.CopyTo(arr, 0);
}
else
{
q.CopyTo(arr, 0);
}
Assert.Equal(q, arr);
if (count > 1)
{
int toRemove = count / 2;
int remaining = count - toRemove;
for (int i = 0; i < toRemove; i++)
{
int item;
Assert.True(q.TryDequeue(out item));
Assert.Equal(i, item);
}
if (viaInterface)
{
c.CopyTo(arr, 1);
}
else
{
q.CopyTo(arr, 1);
}
Assert.Equal(0, arr[0]);
for (int i = 0; i < remaining; i++)
{
Assert.Equal(arr[1 + i], i + toRemove);
}
}
}
示例4: CopyTo_Empty_Success
public void CopyTo_Empty_Success()
{
var q = new ConcurrentQueue<int>();
q.CopyTo(Array.Empty<int>(), 0);
}
示例5: Test5_CtorAndCopyToAndToArray
// Instantiates the queue w/ the enumerator ctor and validates the resulting copyto & toarray.
public static void Test5_CtorAndCopyToAndToArray(int count)
{
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentQueue<int> s = new ConcurrentQueue<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
Assert.Equal(arr.Length, sa1.Length);
for (int i = 0; i < sa1.Length; i++)
{
Assert.Equal(arr[i], sa1[i]);
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
Assert.Equal(arr.Length, sa2.Length);
for (int i = 0; i < sa2.Length; i++)
{
Assert.Equal(arr[i], sa2[i]);
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
Assert.Equal(arr.Length, sa3.Length);
for (int i = 0; i < sa3.Length; i++)
{
Assert.Equal(arr[i], (int)sa3[i]);
}
}