I am trying to modify the value inside the inner list by iterating the list. However, i keep getting the same outcome for the inner list irrespective of the outer list. I expect the outcome of the maxSpeed
is different for different vehicle.
Hope someone could help on this.
Note that this is not a random number generator problem. This is just a sample code that I produce and this issue do exist in my project code without the use of random number generator.
List<Vehicle> mainList = new List<Vehicle>();
List<Properties> defaultPropertyList = new List<Properties>{
new Properties() { maxSpeed = 0, isTwoDoor = true },
new Properties() { maxSpeed = 0, isTwoDoor = true },
};
mainList.Add(
new Vehicle() {
number = 1,
property = new List<Properties>(defaultPropertyList)
}
);
mainList.Add(
new Vehicle() {
number = 2,
property = new List<Properties>(defaultPropertyList)
}
);
foreach(Vehicle vehicle in mainList) {
Random rnd = new Random();
vehicle.property.ForEach(x => x.maxSpeed = rnd.Next(1, 100));
}