C# null coalescing operator returning null

前端 未结 6 2313
小鲜肉
小鲜肉 2021-02-14 10:44

Recently my co-worker showed me a block of code that was not working correctly:

public class SomeClass
{
    private IList _categories;

    publ         


        
6条回答
  •  误落风尘
    2021-02-14 11:18

    If you sure that its because of threading issue, then use the lock keyword. I believe this should work.

    public class SomeClass
    {
        private IList _categories;
    
        public void SetCategories()
        {
            lock(this) 
            {
              _categories = GetCategories() ?? new List();
              DoSomethingElse();
            }
        }
    
        public IList GetCategories()
        {
            return RetrieveCategories().Select(Something).ToList();
        }
    }
    

提交回复
热议问题