3
I have a class Funcionario
which has as attributes: CPF
, Nome
and Salario
.
I have to create an X amount of instances of this class in a List<>
and after that, return to the user the values of this list. With the code I created I did not get the values of the properties of each instance, only the namespace and the class.
aumento_funcionario.funcionario
I’d like to know why. Follow my code.
//Código da classe Program.cs
using System;
using System.Collections.Generic;
using System.Globalization;
namespace aumento_funcionarios
{
class Program
{
static void Main(string[] args)
{
Console.Write("Quantos funcionários serão cadastrados? ");
int qtde_cadastros = int.Parse(Console.ReadLine());
List<funcionario> Lista = new List<funcionario>();
for (int i = 0; i < qtde_cadastros; i++)
{
Console.WriteLine("Dados do " + (i + 1) + "º funcionário: ");
Console.Write("CPF: ");
int cpf = int.Parse(Console.ReadLine());
Console.Write("Nome: ");
string nome = Console.ReadLine();
Console.Write("Salário: ");
double salario = double.Parse(Console.ReadLine());
Lista.Add(new funcionario(cpf, nome, salario));
Console.WriteLine();
}
for (int i = 0; i < Lista.Count; i++)
{
Console.WriteLine(Lista[i]);
}
Console.ReadLine();
}
}
}
//Código classe Funcionarios
using System;
using System.Collections.Generic;
using System.Globalization;
namespace aumento_funcionarios
{
class funcionario
{
public int CPF;
public string Nome;
public double Salario { get; private set; }
public funcionario (int cpf, string nome, double salario)
{
this.CPF = cpf;
this.Nome = nome;
this.Salario = salario;
}
}
}
I believe that with the answers below you have already understood your mistake in finding a solution that suits you. But by convention of the language, your classes should start with a bad letter, then rename to
Funcionario
https://docs.microsoft.com/pt-br/previous-versions/dotnet/netframework-4.0/ms229043(v%3dvs.100)– Leandro Angelo
After, I wouldn’t recommend you use the whole type to store a CPF, if it doesn’t start with 0 it will probably already be a
long
and those who start will make it more difficult for you to consult– Leandro Angelo
Thanks for the tips, @Leandroangelo. Like the
int
in this case fulfilled my need and at the moment I am not worried about the performance of my code so I ended up using theint
same. But as I advance I will choose the types of variables more carefully.– Edinaldo Ribeiro
I also have to study more about the patterns camelCase and Pascalcase. Adopting a standard at the beginning of my studies will be fundamental further forward.
– Edinaldo Ribeiro