Created
November 6, 2017 20:09
-
-
Save Fhernd/30beb51329ec94e1e8f07a5cd77cf824 to your computer and use it in GitHub Desktop.
Ejemplo de uso de la clase HashSet<T>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace cap07.colecciones | |
{ | |
public class UnionConjuntos | |
{ | |
public static void Main() | |
{ | |
HashSet<int> pares = new HashSet<int>(); | |
HashSet<int> impares = new HashSet<int>(); | |
for(int i = 1; i <= 10; ++i) | |
{ | |
if(i % 2 == 0 ){ | |
pares.Add(i); | |
} else { | |
impares.Add(i); | |
} | |
} | |
Console.WriteLine("Contenido conjunto números pares:"); | |
MostrarConjunto(pares); | |
Console.WriteLine("\nContenido conjunto números impares:"); | |
MostrarConjunto(impares); | |
HashSet<int> numerosEnteros = new HashSet<int>(pares); | |
numerosEnteros.UnionWith(impares); | |
Console.WriteLine("\nConjunto resultante de la unión de pares e impares:"); | |
MostrarConjunto(numerosEnteros); | |
} | |
public static void MostrarConjunto(HashSet<int> conjunto) | |
{ | |
foreach(int numero in conjunto) | |
{ | |
Console.Write("{0} ", numero); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment