0
I am doing a list exercise where I have to enter the employee id, but there should be no repetition of id. I would like to know how to use the foreach
to avoid checking and blocking if a repeated value is inserted.
Follows the code of the executable class:
Console.WriteLine("Quantos empregados serão registrados?");
int n = int.Parse(Console.ReadLine());
List<Empregado> lista = new List<Empregado>();
for(int i=1; i<=n; i++)
{
Console.WriteLine("Empregado #{0}",i);
Console.WriteLine("Digite o ID:");
int id = int.Parse(Console.ReadLine());
foreach (int obj in lista)
{
if (id!=null)
{
Console.WriteLine("Id já existe, digite outro:");
id = int.Parse(Console.ReadLine());
}
}
And can’t it be done any other way? Has some restriction of what you can use?
– Maniero
I asked the teacher and he answered this:"What you can do is scroll through the list to see if there is already the id being informed. For this, you can make a foreach loop in the list to perform this validation, beauty?" But I didn’t understand how to do this.
– Victor Marcantonio
But this is a bad way to do, there is some restriction?
– Maniero
if there is an easier way can be tbm, just wanted to know how to avoid the same duplicity
– Victor Marcantonio