8
How do I update a specific element of a generic list by locating by ID and passing an updated object in its place, updating the name and email?
class Program
{
static void Main(string[] args)
{
List<Aluno> aluno = new List<Aluno>{
new Aluno() { AlunoId = 1, Nome = "Cláudia",Email="[email protected]" },
new Aluno() { AlunoId = 2, Nome = "Pedro",Email="[email protected]" },
new Aluno() { AlunoId = 3, Nome = "Eduardo",Email="[email protected]" }
};
Console.WriteLine("==================================");
foreach (var item in aluno)
{
Console.WriteLine("ID: {0}\nNome: {1}\nEmail: {2}", item.AlunoId, item.Nome,item.Email);
Console.WriteLine("==================================");
}
Console.WriteLine("\nLista Atualizada\n");
int iElemento = 1;
var elem = aluno.Where<Aluno>(a => a.AlunoId == iElemento).FirstOrDefault();
int index = aluno.IndexOf(elem);
aluno[index].Nome = "Cláudia Limeira";
aluno[index].Email = "[email protected]";
foreach (var item in aluno)
{
Console.WriteLine("ID: {0}\nNome: {1}\nEmail: {2}", item.AlunoId, item.Nome, item.Email);
Console.WriteLine("==================================");
}
Console.Read();
}
}
class Aluno
{
public int AlunoId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
}
I didn’t say anything stupid when I said type by reference in my answer, right?
– Jéf Bueno
No, it’s all right.
– Maniero
Bigown, it was great, I will market as a response!!! Do you think the way I did ended up using too much unnecessary code? My idea as I passed the message was to create an instance of the student class, pass the values to the properties and then locate the element in the list and put that object in the position of the outdated element, doing this automatically, but I ended up extending the code.
– Kelly Soares
Almost nothing at all. But I like to make it as short as possible to keep it legible. And I like to use the most modern things. Some are not available on older compilers, which may require adaptation. One of the things I would certainly do anyway at the time there were two impressions from the list, is to turn this into method to meet the DRY. Or even to meet the principle of sole responsibility.
– Maniero
There are always several ways to do it. If your exercise required doing it by the index, I would do it, but it doesn’t seem to be the case. If it required creating a new instance of
Aluno
and replace the old one with the new one, I would have done so. But it is not specified that in the question, I think it is free to do as you wish, so I opted for the simplest and logical.– Maniero
@Kellysoares I saw that you changed the acceptance of the answer, you know you can only accept one answer? You can vote for all of them, but you take only one, when you take the second, you change the vote. If you hadn’t figured it out yet, take a look at all your questions and look, and you didn’t change your acceptance by accident. You can accept whatever you think is best, this is your own decision, but rest assured that you are actually accepting what you have chosen.
– Maniero
I commented about passing an updated object because I found strange the part where I had to pass properties values by properties, this example I did actually I will adapt to a code that already receives the student class with its updated properties, What I need to do is put this property on the list to make it simpler. The questions I had was to locate an item by ID and update the list with the object that already receives the updated information. But I can create a new question to end this last question of passing the object directly to the located element.
– Kelly Soares
You talk about clicking the triangle where you accept the question as useful or the triangle that points up and you have the option below mark as answer?
– Kelly Soares
@Kellysoares speak on the mark of check in green, can only one per your question. See the [tour]. The triangle up and p/ down, is vote, this you can vote on everything in the whole site, are not in your questions, and if you want to learn I suggest reading other questions, vote on them, vote on the answers you find useful. There is a lot of good material here to learn. Example to start: http://answall.com/questions/tagged/c%23? Sort=votes&pageSize=50
– Maniero
Let’s go continue this discussion in chat.
– Kelly Soares