本文整理汇总了C#中Bag.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# Bag.Clear方法的具体用法?C# Bag.Clear怎么用?C# Bag.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bag
的用法示例。
在下文中一共展示了Bag.Clear方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Simple
public void Simple()
{
var bag1 = new Bag<int> { 3, 4, 5, 6 };
var bag2 = new Bag<int> { 3, 4, 5 };
var shouldBe = new Bag<int> { 6 };
var resultBag = bag1 - bag2;
Assert.IsTrue(resultBag.Equals(shouldBe));
bag1.Clear();
bag2.Clear();
bag1.Add(3, 3);
bag2.Add(3, 2);
bag1.Add(5, 5);
bag2.Add(5, 7);
shouldBe.Clear();
shouldBe.Add(3, 1);
resultBag = bag1 - bag2;
Assert.IsTrue(resultBag.Equals(shouldBe));
}
示例2: Interface
public void Interface()
{
var bag1 = new Bag<int> { 3, 4, 5, 6 };
var bag2 = new Bag<int> { 3, 4, 5 };
var shouldBe = new Bag<int> { 6 };
var resultBag = (Bag<int>)((IBag<int>)bag1).Subtract(bag2);
Assert.IsTrue(resultBag.Equals(shouldBe));
bag1.Clear();
bag2.Clear();
bag1.Add(3, 3);
bag2.Add(3, 2);
bag1.Add(5, 5);
bag2.Add(5, 7);
shouldBe.Clear();
shouldBe.Add(3, 1);
resultBag = bag1.Subtract(bag2);
Assert.IsTrue(resultBag.Equals(shouldBe));
}
示例3: TestPerformance
public void TestPerformance()
{
global::System.Diagnostics.Debug.WriteLine("Number of elements: ");
// Identify max mem size.
Bag<int> bigBag = new Bag<int>();
int maxMem = 5000;
///pointless to use int.maxvalue (sometimes it works, some it does not ... depends on other process)
for (int index = 0; index < 5000; ++index)
{
try
{
bigBag.Add(index);
}
catch (Exception)
{
maxMem = index ; //some extra to be sure (there are some memory allocs we cant control in other threads)
break;
}
}
bigBag = null;
// This is need to secure that enough memory is left.
GC.Collect();
GC.WaitForPendingFinalizers();
#if !METRO
GC.WaitForFullGCComplete();
#endif
GC.Collect();
global::System.Diagnostics.Debug.WriteLine(maxMem.ToString(CultureInfo.InvariantCulture));
// Reset bag.
bigBag = new Bag<int>(0);
// Start measurement.
Stopwatch stopwatch = Stopwatch.StartNew();
// Fill
for (int index = maxMem; index >= 0; --index)
{
bigBag.Add(index);
}
stopwatch.Stop();
global::System.Diagnostics.Debug.WriteLine("Load duration: {0}", FastDateTime.ToString(stopwatch.Elapsed));
stopwatch.Restart();
bigBag.Clear();
stopwatch.Stop();
global::System.Diagnostics.Debug.WriteLine("Clear duration: {0}", FastDateTime.ToString(stopwatch.Elapsed));
}
示例4: TestClear
public void TestClear()
{
Bag<string> target = new Bag<string>(Capacity) { TestElement1, TestElement2, TestElement3 };
target.Clear();
Assert.AreEqual(0, target.Count);
}
示例5: TestPerformance
public void TestPerformance()
{
Debug.WriteLine("Number of elements: ");
// Identify max mem size.
Bag<int> bigBag = new Bag<int>();
int maxMem = 500000;
// pointless to use int.maxvalue (sometimes it works, some it does not ... depends on other process)
for (int index = 0; index < maxMem; ++index)
{
try
{
bigBag.Add(index);
}
catch (Exception)
{
// some extra to be sure (there are some memory allocs we cant control in other threads)
maxMem = index;
break;
}
}
bigBag = null;
// This is need to secure that enough memory is left.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Debug.WriteLine(maxMem.ToString(CultureInfo.InvariantCulture));
// Reset bag.
bigBag = new Bag<int>(0);
// Start measurement.
Stopwatch stopwatch = Stopwatch.StartNew();
// Fill
for (int index = maxMem; index >= 0; --index)
{
bigBag.Add(index);
}
stopwatch.Stop();
string s1 = string.Format("Load duration: {0}" ,stopwatch.Elapsed.TotalMilliseconds);
stopwatch.Restart();
bigBag.Clear();
stopwatch.Stop();
string s2 = string.Format("Clear duration: {0}", stopwatch.Elapsed.TotalMilliseconds);
Assert.Fail(s1 + "\n" + s2);
}
示例6: TestAddAndRemove
public void TestAddAndRemove()
{
Bag<string> b = new Bag<string>();
Assert.AreEqual(0, b.Count);
Assert.IsFalse(b.Contains("foo"));
Assert.IsFalse(b.Contains("bar"));
Assert.AreEqual(0, b.Occurrences("foo"));
Assert.AreEqual(0, b.Occurrences("bar"));
b.Add("foo");
Assert.IsTrue(b.Contains("foo"));
Assert.AreEqual(1, b.Count);
Assert.AreEqual(1, b.Occurrences("foo"));
Assert.AreEqual(0, b.Occurrences("bar"));
b.Add("bar");
Assert.IsTrue(b.Contains("foo"));
Assert.IsTrue(b.Contains("bar"));
Assert.AreEqual(2, b.Count);
Assert.AreEqual(1, b.Occurrences("foo"));
Assert.AreEqual(1, b.Occurrences("bar"));
b.Add("foo");
Assert.AreEqual(3, b.Count);
Assert.IsTrue(b.Contains("foo"));
Assert.IsTrue(b.Contains("bar"));
Assert.IsFalse(b.Contains("baz"));
Assert.AreEqual(2, b.Occurrences("foo"));
Assert.AreEqual(1, b.Occurrences("bar"));
Assert.IsTrue(b.Remove("foo"));
Assert.AreEqual(2, b.Count);
Assert.IsTrue(b.Contains("foo"));
Assert.AreEqual(1, b.Occurrences("foo"));
Assert.AreEqual(1, b.Occurrences("bar"));
Assert.IsTrue(b.Remove("foo"));
Assert.AreEqual(1, b.Count);
Assert.IsFalse(b.Contains("foo"));
Assert.AreEqual(0, b.Occurrences("foo"));
Assert.AreEqual(1, b.Occurrences("bar"));
Assert.IsFalse(b.Remove("foo"));
Assert.AreEqual(1, b.Count);
Assert.IsFalse(b.Contains("foo"));
Assert.AreEqual(0, b.Occurrences("foo"));
Assert.AreEqual(1, b.Occurrences("bar"));
b.Clear();
Assert.AreEqual(0, b.Count);
Assert.IsFalse(b.Contains("foo"));
Assert.AreEqual(0, b.Occurrences("foo"));
Assert.AreEqual(0, b.Occurrences("bar"));
}
示例7: ClearExample
public void ClearExample()
{
var bag = new Bag<string> {"cat", "dog", "canary"};
// There should be 3 items in the bag.
Assert.AreEqual(3, bag.Count);
// Clear the bag
bag.Clear();
// The bag should be empty.
Assert.AreEqual(0, bag.Count);
// No cat here..
Assert.IsFalse(bag.Contains("cat"));
}
示例8: IsEmptyExample
public void IsEmptyExample()
{
var bag = new Bag<string>();
// Bag will be empty initially
Assert.IsTrue(bag.IsEmpty);
bag.Add("cat");
// Bag will be not be empty when an item is added
Assert.IsFalse(bag.IsEmpty);
bag.Clear();
// Bag will be empty when items are cleared
Assert.IsTrue(bag.IsEmpty);
}