Is List thread-safe for reading?

后端 未结 4 1010
遥遥无期
遥遥无期 2020-12-17 08:08

Is the following pseudocode thread-safe ?

IList dataList = SomeNhibernateRepository.GetData();

Parallel.For(..i..)
{
    foreach(var item in dataLi         


        
4条回答
  •  [愿得一人]
    2020-12-17 08:35

    to make sure no one is gonna change yuor list you could access it through an IEnumerable

    IEnumerable dataList = SomeNhibernateRepository.GetData();
    
    Parallel.For(..i..)
    {
        foreach(var item in dataList)
        {
           DoSomething(item);
        }
    }
    

提交回复
热议问题