Answer the question
In order to leave comments, you need to log in
How to change the values of an object from a list if the list contains it?
There is a list of objects.
class Program
{
List<Item> items = new List<Item>;
items.Add(new Item(){ItemName = "Ball", ItemID = 10, ItemCost = 3})
items.Add(new Item(){ItemName = "Toy", ItemID = 14, ItemCost = 15})
}
public class Item
{
public string ItemName { get; set;}
public int ItemID { get; set; }
public double ItemCost { get; set;}
}
Answer the question
In order to leave comments, you need to log in
public class Item
{
public Item(string name, int id, double cost)
{
Name = name;
Id = id;
Cost = cost;
}
public string Name { get; set; }
public int Id { get; set; }
public double Cost { get; set; }
public override string ToString()
{
return $"Id: {Id}, Name: {Name}, Cost: {Cost}";
}
}
static void Main(string[] args)
{
var findName = "Ball";
var items = new List<Item>
{
new Item("Ball", 10, 3),
new Item("Toy", 14, 15)
};
var item = items.FirstOrDefault(product => product.Name.Contains(findName));
if (item != null)
{
item.Cost = 100;
item.Id = 999;
}
Console.WriteLine(string.Join(Environment.NewLine, items));
Console.ReadKey();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question