This is a very similar problem to here c# creating new instances of a object in a for loop but my problem is unfortunately slightly different. I'm not using random either.
I have a setup similar to this:
public List<Candle> GetCandles(int can)
{
List<Candle> dpl = new List<Candle>();
for (int i = 0; i < can; i++)
{
Candle dp = new Candle();
dp.x = new Random().Next(0000, 9999);
dp.y = new Random().Next(0000, 9999);
dpl.Add(dp);
}
return dpl;
}
My problem is that at the end of it, I don't have separate instances of candles inside. They're all the same candle for each element - despite me using "new" each time.
How do I make 100% sure that I'm putting a brand new candle into the list?
EDIT: Thanks to all that answered, but I was able to find out my own problem. Inside the class I was using, the variables were static. That meant that every single class instance had the same value. So if I set A to equal 50, all of the classes had 50.
I'm sorry if it wasn't clear, but that code wasn't mine. I'm not using random, I just posted the example because it was similar to the setup I had in my code. I had a loop and a list and was adding instances to it.