本文整理汇总了C#中System.Collections.Generic.HashSet<T>.HashSet构造函数的典型用法代码示例。如果您正苦于以下问题:C# HashSet<T>构造函数的具体用法?C# HashSet<T>怎么用?C# HashSet<T>使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Collections.Generic.HashSet<T>
的用法示例。
在下文中一共展示了HashSet<T>构造函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
示例2: for
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
DisplaySet(oddNumbers);
// Create a new HashSet populated with even numbers.
HashSet<int> numbers = new HashSet<int>(evenNumbers);
Console.WriteLine("numbers UnionWith oddNumbers...");
numbers.UnionWith(oddNumbers);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
void DisplaySet(HashSet<int> collection)
{
Console.Write("{");
foreach (int i in collection)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" }");
}
输出:
* evenNumbers contains 5 elements: { 0 2 4 6 8 } * oddNumbers contains 5 elements: { 1 3 5 7 9 } * numbers UnionWith oddNumbers... * numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
示例3: foreach
HashSet<string> allVehicles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
List<string> someVehicles = new List<string>();
someVehicles.Add("Planes");
someVehicles.Add("Trains");
someVehicles.Add("Automobiles");
// Add in the vehicles contained in the someVehicles list.
allVehicles.UnionWith(someVehicles);
Console.WriteLine("The current HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
allVehicles.Add("Ships");
allVehicles.Add("Motorcycles");
allVehicles.Add("Rockets");
allVehicles.Add("Helicopters");
allVehicles.Add("Submarines");
Console.WriteLine("\nThe updated HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Verify that the 'All Vehicles' set contains at least the vehicles in
// the 'Some Vehicles' list.
if (allVehicles.IsSupersetOf(someVehicles))
{
Console.Write("\nThe 'All' vehicles set contains everything in ");
Console.WriteLine("'Some' vechicles list.");
}
// Check for Rockets. Here the OrdinalIgnoreCase comparer will compare
// true for the mixed-case vehicle type.
if (allVehicles.Contains("roCKeTs"))
{
Console.WriteLine("\nThe 'All' vehicles set contains 'roCKeTs'");
}
allVehicles.ExceptWith(someVehicles);
Console.WriteLine("\nThe excepted HashSet contains:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Remove all the vehicles that are not 'super cool'.
allVehicles.RemoveWhere(isNotSuperCool);
Console.WriteLine("\nThe super cool vehicles are:\n");
foreach (string vehicle in allVehicles)
{
Console.WriteLine(vehicle);
}
// Predicate to determine vehicle 'coolness'.
bool isNotSuperCool(string vehicle)
{
bool superCool = (vehicle == "Helicopters") || (vehicle == "Motorcycles");
return !superCool;
}
输出:
The current HashSet contains: Planes Trains Automobiles The updated HashSet contains: Planes Trains Automobiles Ships Motorcycles Rockets Helicopters Submarines The 'All' vehicles set contains everything in 'Some' vechicles list. The 'All' vehicles set contains 'roCKeTs' The excepted HashSet contains: Ships Motorcycles Rockets Helicopters Submarines The super cool vehicles are: Motorcycles Helicopters