I try to run this code but the result I got that not as expected
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Test
{
public class Program
{
public static void Main(string[] args)
{
var tasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
var task = Task.Run(() =>
{
Console.WriteLine(i);
});
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine("done");
}
}
}
I expect the result is a list of 100 numbers 0 - 99 and not have duplicates
But I got a list that missing and duplicate like 6677899999...99 (miss 0 - 5 and duplicates) on https://dotnetfiddle.net/
I get a list of "100" if running on visual studio
So why this happen? Why is there a difference between visual studio and https://dotnetfiddle.net/? How to fix this?