C#空合并运算符返回null [英] C# null coalescing operator returning null
问题描述
最近我的同事向我展示了code块,这不是正常工作:
Recently my co-worker showed me a block of code that was not working correctly:
public class SomeClass
{
private IList<Category> _categories;
public void SetCategories()
{
_categories = GetCategories() ?? new List<Category>();
DoSomethingElse();
}
public IList<Category> GetCategories()
{
return RetrieveCategories().Select(Something).ToList();
}
}
(我知道,运营商是多余的,因为LINQ了ToList总是会返回一个列表,但这是code的设置方式)。
(I am aware that the operator is superfluous since the linq ToList will always return a list, but that is how the code was set up).
但问题是,_categories是空。在调试器,设置在 _categories = GetCategories断点()?新的List&LT;类别&GT;()
,然后踩着过来DoSomethingElse()_ 类别仍然为空的
The problem was that _categories was null. In the debugger, setting a breakpoint on _categories = GetCategories() ?? new List<Category>()
, then stepping over to DoSomethingElse(), _categories would still be null.
直接设置_categories到GetCategories()工作的罚款。拆分?在一个完整的if语句工作得很好。空合并经营者也没有。
Directly setting _categories to GetCategories() worked fine. Splitting the ?? in to a full if statement worked fine. The null coalescing operator did not.
这是一个ASP.NET应用程序,因此在不同的线程也可能会被干扰,但是这是他的机器上,只有他在一个浏览器连接。 _cateogories也不是一成不变的,或任何东西。
It is an ASP.NET application, so a different thread could possibly be interfering, but it was on his machine, with only him connected in a browser. _cateogories is not static, or anything.
我不知道是的怎么会这样可能发生?的
编辑:
我想补充的离奇, _categories
从未设置除了该功能在任何地方的(除了初始化类的)。
Just to add to the bizarreness, _categories
is never set anywhere besides that function (apart from initializing the class).
确切的code是像这样:
The exact code is like so:
public class CategoryListControl
{
private ICategoryRepository _repo;
private IList<Category> _categories;
public override string Render(/* args */)
{
_repo = ServiceLocator.Get<ICategoryRepository>();
Category category = _repo.FindByUrl(url);
_categories = _repo.GetChildren(category) ?? new List<Category>();
Render(/* Some other rendering stuff */);
}
}
public class CategoryRepository : ICategoryRepository
{
private static IList<Category> _categories;
public IList<Category> GetChildren(Category parent)
{
return _categories.Where(c => c.Parent == parent).ToList<Category>();
}
}
即使它的GetChildren神奇地返回null,CategoryListControl._categories还是应该永远不会为null。的GetChildren应该也永远不会回来,因为IEnumerable.ToList()的空。
Even it GetChildren magically returned null, CategoryListControl._categories still should never, ever be null. GetChildren should also never, ever return null because of IEnumerable.ToList().
编辑2:
试行@ smartcaveman的code,我发现这样的:
Trying out @smartcaveman's code, I found out this:
Category category = _repo.FindByUrl(url);
_categories = _repo.GetChildren(category) ?? new List<Category>();
_skins = skin; // When the debugger is here, _categories is null
Renderer.Render(output, _skins.Content, WriteContent); // When the debugger is here, _categories is fine.
同时,在测试时如果(_categories == NULL)抛出新的异常()
,_categories是空的if语句,然后跨过的异常没有抛出
As well, when testing if(_categories == null) throw new Exception()
, _categories was null on the if statement, then stepping over the exception was not thrown.
所以,看起来这是一个调试器的bug。
So, it seems like this is a debugger bug.
推荐答案
这可能是与调试器,而不是用code的一个问题。尝试打印出的价值或聚结运营商的声明后,做一个空检查。
This could be a problem with the debugger rather than with the code. Trying printing out the value or doing a null check after the statement with the coalesce operator.
这篇关于C#空合并运算符返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!