3
I have a class called Pessoa
, this class has two properties Nome
and Idade
, I am using a List<T>
to manipulate data, I know I can manipulate data like string
or int
creating a List<string> listStr
or a List<int> listInt
. And if I want to insert a new value of the data type corresponding to the created list just use the method Add()
. For example:
listStr.Add("Minha Lista 1");
And also for the guy int
:
listInt.Add(50);
However, I don’t know how to access and enter values in the properties Nome
and Idade
in instances of my class Pessoa
who are in a List<Pessoa>
, tried to use the method ListPessoa.Add()
, however it seems to accept only objects of the type Pessoa
. Below follows an example as an illustration of the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListaObjeto
{
class Pessoa
{
private String nome;
private int idade;
public Pessoa()
{
this.nome = String.Empty;
this.idade = 0;
}
public Pessoa(string nome, int idade)
{
this.nome = nome;
this.idade = idade;
}
public string Nome
{
get { return this.nome; }
set { this.nome = value; }
}
public int Idade
{
get { return this.idade; }
set { this.idade = value; }
}
}
class Program
{
static void Main(string[] args)
{
List<Pessoa> ListaPessoa = new List<Pessoa>();
// Como acessar as propriedades Nome e Idade das instância de Pessoa que estão na ListaPessoa?
}
}
}
Thanks for the reply. Stop serving the $ character before the string?
– gato
@Denercarvalho the character
$
is part of a new functionality of version 6 of C#, this functionality is called string interpolation. Creating an "interpolated string" works in a very similar way to creating a string through the methodstring.Format
and can generate the same result only through a code more readable and easy to write.– Zignd
Yes, I tried to use here but the compiler pointed error, I am using the visual studio 2012.
– gato
@DenerCarvalho http://answall.com/q/91117/101
– Maniero