本文整理汇总了C#中HashedSet.UnionWith方法的典型用法代码示例。如果您正苦于以下问题:C# HashedSet.UnionWith方法的具体用法?C# HashedSet.UnionWith怎么用?C# HashedSet.UnionWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashedSet
的用法示例。
在下文中一共展示了HashedSet.UnionWith方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestUnionMethod
public void TestUnionMethod()
{
var hashedSet = new HashedSet<int>();
Assert.IsTrue(hashedSet.Add(1));
Assert.IsTrue(hashedSet.Add(2));
Assert.AreEqual(2, hashedSet.Count);
int[] unionArray = { 2, 3, 4, 5 };
hashedSet.UnionWith(unionArray);
Assert.AreEqual(5, hashedSet.Count);
CollectionAssert.AreEqual(hashedSet.Keys.ToList(), new List<int>() { 1, 2, 3, 4, 5 });
}
示例2: Main
static void Main()
{
HashedSet<int> set = new HashedSet<int>();
set.Add(5);
set.Add(5);
set.Add(6);
set.Add(1);
set.Add(3);
set.Add(3);
set.Add(8);
set.Remove(6);
Console.WriteLine("First set contains 6 - {0}", set.Contains(6));
Console.WriteLine("First set contains 8 - {0}", set.Contains(8));
Console.WriteLine("First set");
foreach (var item in set)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
Console.WriteLine("Second set");
HashedSet<int> set2 = new HashedSet<int>();
set2.Add(13);
set2.Add(3);
set2.Add(1);
foreach (var item in set2)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
HashedSet<int> union = set.UnionWith(set2);
Console.WriteLine("Union: ");
foreach (var item in union)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
HashedSet<int> intersection = set.IntersectWith(set2);
Console.WriteLine("Intersection: ");
foreach (var item in intersection)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
}
示例3: Main
static void Main()
{
HashedSet<int> hashSet1 = new HashedSet<int>();
for (int i = 0; i < 10; i++)
{
hashSet1.Add(i);
}
Console.WriteLine("Find element with key = 4");
Console.WriteLine(hashSet1.Find(4));
Console.WriteLine("Find element with key = 2 and write the count of elements");
hashSet1.Remove(2);
Console.WriteLine(hashSet1.Count);
HashedSet<int> hashSet2 = new HashedSet<int>();
hashSet2.Add(5);
hashSet2.Add(9);
hashSet2.Add(33);
Console.WriteLine("Union: ");
hashSet1.UnionWith(hashSet2);
for (int i = 0; i < hashSet1.Container.Count; i++)
{
Console.WriteLine(hashSet1.Container.Keys[i]);
}
Console.WriteLine();
Console.WriteLine("Intersect: ");
hashSet1.IntersectWith(hashSet2);
for (int i = 0; i < hashSet1.Container.Count; i++)
{
Console.WriteLine(hashSet1.Container.Keys[i]);
}
Console.WriteLine();
Console.WriteLine("count after clear");
hashSet2.Clear();
Console.WriteLine(hashSet2.Container.Count);
}
示例4: UnionShouldReturnSetWithElementsFromBothSets
public void UnionShouldReturnSetWithElementsFromBothSets()
{
var firstSet = new HashedSet<int>();
var secondSet = new HashedSet<int>();
for (int i = 0; i < 10; i++)
{
if (i < 7)
{
firstSet.Add(i);
}
if (i > 3)
{
secondSet.Add(i);
}
}
var result = firstSet.UnionWith(secondSet);
Assert.AreEqual(10, result.Count);
foreach (var item in result)
{
Assert.IsTrue(item >= 0 && item < 10);
}
}
示例5: Union
public void Union()
{
int[] oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
int[] digits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
HashedSet<int> setOdds = new HashedSet<int>(oddNumbers);
HashedSet<int> setDigits = new HashedSet<int>(digits);
setOdds.UnionWith(setDigits);
int[] expectedArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
int[] actualArray = setOdds.ToArray();
Array.Sort(actualArray);
CollectionAssert.AreEqual(expectedArray, actualArray);
setOdds = new HashedSet<int>(oddNumbers);
setDigits.UnionWith(setOdds);
actualArray = setDigits.ToArray();
Array.Sort(actualArray);
CollectionAssert.AreEqual(expectedArray, actualArray);
setOdds.UnionWith(setOdds);
Assert.AreEqual(oddNumbers.Length, setOdds.Count);
}
示例6: UnionShouldReturnUniqueValues
public void UnionShouldReturnUniqueValues()
{
ICollection<int> numbers = new List<int>() { 1, 2, 2, 2, 3, 3, 4, 4, 5 };
var hashedset = new HashedSet<int>();
new int[] { 6, 2, 6, 7, 3 }.ForEach(n => hashedset.Add(n));
var result = hashedset.UnionWith(numbers);
Assert.AreEqual(7, result.Count);
}
示例7: UnionShouldProduceSetContainingElementsFromBothSets
public void UnionShouldProduceSetContainingElementsFromBothSets()
{
ICollection<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
var hashedset = new HashedSet<int>();
sampleNames.ForEach(n => hashedset.Add(n.Length));
var result = hashedset.UnionWith(numbers);
numbers.ForEach(x => Assert.IsTrue(result.Contains(x)));
hashedset.ForEach(x => Assert.IsTrue(result.Contains(x)));
}
示例8: Main
static void Main()
{
HashedSet<string> myHashSet = new HashedSet<string>();
// Add method test
Console.WriteLine("Add and foreach tests");
myHashSet.Add("kir4o");
myHashSet.Add("lili");
myHashSet.Add("tanq");
Console.WriteLine();
foreach (string element in myHashSet)
{
Console.WriteLine(element);
}
Console.WriteLine();
// Find method test
Console.WriteLine("Find method test");
Console.WriteLine("Find tanq = {0}", myHashSet.Find("tanq"));
Console.WriteLine("Find sdfasdf = {0}", myHashSet.Find("sdfasdf"));
Console.WriteLine();
// Remove method test
Console.WriteLine("Remove method test");
myHashSet.Remove("lili");
Console.WriteLine("lili removed");
Console.WriteLine();
// Union with test
HashedSet<string> otherHashSet = new HashedSet<string>();
otherHashSet.Add("venera");
otherHashSet.Add("jupiter");
otherHashSet.Add("kir4o");
Console.WriteLine("Union with:");
foreach (string element in otherHashSet)
{
Console.WriteLine(element);
}
myHashSet.UnionWith(otherHashSet);
Console.WriteLine("Resulted hashSet:");
foreach (string element in myHashSet)
{
Console.WriteLine(element);
}
Console.WriteLine();
// Intersect with test
Console.WriteLine("Intersect with :");
foreach (string element in otherHashSet)
{
Console.WriteLine(element);
}
myHashSet.IntersectWith(otherHashSet);
Console.WriteLine();
Console.WriteLine("Resulted hashSet:");
foreach (string element in myHashSet)
{
Console.WriteLine(element);
}
}