Revolt Revolt - 1 year ago 134
C# Question

Add item to existing Collection C#

I have Collection:

public class Test
public double Index { set; get; }
public double A { set; get; }
public double B { set; get; }
public double C { set; get; }
List<Test> test = new List<Test>();

I filled A and B with some random numbers. After that, I want add items C in collection. For example, what I trying:

foreach (Test t in test)
c = t.A + t.B;
test.Add(new Test {C = c }); <------

How can I add element C on same position like A and B? (Sorry for my english)

Answer Source
foreach (Test t in test)
    t.C = t.A + t.B;

just set the property to the equation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download