Error performing averaging

1

I would like to take the sum of all the wages and divide by the amount of employees, but this one giving error, when compilo says that it has 0 in the variables Count of the list and total.

public class Informacao
{
    public float salBruto { get; set; }
    public int numFilhos { get; set; }
}
class Ex2
{
    List<Informacao> info = new List<Informacao>();
    public void CadNovaPesquisa()
    {
        Informacao infoo = new Informacao();

        Console.Clear();

        Console.WriteLine("Informe seu salário bruto:");
        infoo.salBruto = float.Parse(Console.ReadLine());
        Console.WriteLine("Informe a quantidade de filhos:");
        infoo.numFilhos = Convert.ToInt32(Console.ReadLine());
        info.Add(infoo);
    }
    public void CalculaMedia()
    {
        int i = info.Count;
        float total;
        total = info.Sum(x => x.salBruto);
        Console.WriteLine(i);
        //float media = total / i;
        Console.WriteLine($"TOTAL: {total}");
        Console.ReadLine();

    }
    
asked by anonymous 01.04.2018 / 21:02

1 answer

1

I did not try to see the error since it has other problems in the code, writing correctly works, although I would change several other things:

using static System.Console;
using System.Collections.Generic;
using System.Linq;

public class Program {
    private static List<Informacao> informacoes = new List<Informacao>();
    public static void Main() {
        CadNovaPesquisa();
        CadNovaPesquisa();
        CalculaMedia();
    }
    public static void CadNovaPesquisa()   {
        var informacao = new Informacao();
        WriteLine("Informe seu salário bruto:");
        if (!decimal.TryParse(ReadLine(), out var salBruto)) return;
        informacao.SalBruto = salBruto;
        WriteLine("Informe a quantidade de filhos:");
        if (!int.TryParse(ReadLine(), out var numFilhos)) return;
        informacao.NumFilhos = numFilhos;
        informacoes.Add(informacao);
    }
    public static void CalculaMedia() {
        var total = informacoes.Sum(x => x.SalBruto);
        WriteLine($"TOTAL: {total} MEDIA: {total/informacoes.Count}");
    }
}

public class Informacao {
    public decimal SalBruto { get; set; }
    public int NumFilhos { get; set; }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
01.04.2018 / 22:16