How to simplify code with Foreach List?

Asked

Viewed 219 times

5

How to use the ForEach() of List in the implementation below

using System.Collections.Generic;

public class Program
{
    public static void Main()
    {   
        List<Pessoa> pessoas = new List<Pessoa>();
        pessoas.Add(new Pessoa(){Nome = "José" , Sexo = "M"});
        pessoas.Add(new Pessoa(){Nome = "Pedro", Sexo = "M"});
        pessoas.Add(new Pessoa(){Nome = "João" , Sexo = "M"});
        pessoas.Add(new Pessoa(){Nome = "Maria", Sexo = "F"});

        bool temMulher = new VerificaSeHaMulher(pessoas).Verifica();
    }
}

class VerificaSeHaMulher
{
    List<Pessoa> pessoas;

    public VerificaSeHaMulher(List<Pessoa> pessoas)
    {
        this.pessoas = pessoas;
    }

    public bool Verifica()
    {
        foreach(Pessoa pessoa in pessoas)
          if(pessoa.Sexo.Equals("F"))
            return true;

        return false;
    }
}

class Pessoa
{
    public string Nome;
    public string Sexo;
}

In this case, I want to make the implementation of the method cleaner Verifica.

  • Is there anything specific that you think is bad? It seems pretty simple. Is there a need to be a class? Nothing wrong with that, as long as you need it. Maybe you’re using a Strategy Pattern or variation of it somewhere. But if you’re using it by accident, you should simplify it. Otherwise I would only change the syntax of the comparison so as not to look like Java. Where lambda gets into that story?

  • Had put the lambda by ignorance. Adjusted

  • 1

    The confusion makes sense. See: http://answall.com/q/79894/101 and http://answall.com/q/22265/101 and http://answall.com/q/14212/101

  • One option to simplify code is the use of resharper is a paid component plus is good for this type of help. https://www.jetbrains.com/resharper/

  • @Guilhermesilva, I advise you to read "foreach" vs "foreach" and Where is List<T>. Foreach().

1 answer

2


I would change some things more by style. But it’s my taste, doesn’t mean it’s clearly better.

Apart from this I would wonder if this method should not be within another class. It’s useful to use this way, depending on your requirements, but if you can’t tell what a class this is for, you’re decking the peacock.

using System.Collections.Generic;

public class Program {
    public static void Main() {   
        var pessoas = new List<Pessoa>() {
            new Pessoa() {Nome = "José" , Sexo = "M"},
            new Pessoa() {Nome = "Pedro", Sexo = "M"},
            new Pessoa() {Nome = "João" , Sexo = "M"},
            new Pessoa() {Nome = "Maria", Sexo = "F"}
        };
        bool temMulher = new VerificaSeHaMulher(pessoas).Verifica();
    }
}

class VerificaSeHaMulher {
    private readonly List<Pessoa> pessoas;
    public VerificaSeHaMulher(List<Pessoa> pessoas) => this.pessoas = pessoas;

    public bool Verifica() {
        foreach (var pessoa in pessoas) if (pessoa.Sexo == "F") return true;
        return false;
    }
}

class Pessoa {
    public string Nome;
    public string Sexo;
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

If you want to use LINQ just do this:

return pessoas.Any(p => p.Sexo == "F");

Behold working in the .NET Fiddle.

Documentation of Any().

  • Man, that’s what I needed, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.