我有以下方法:
internal static void removeMethodFromTag(byte p, Method method)
{
Console.WriteLine("Remove method " + method.getName() + " from the tag");
List<Method> outList = new List<Method>();
methodTaggings.TryGetValue(p, out outList);
Console.WriteLine(outList.Count);
outList.Remove(method);
methodTaggings.Remove(p);
methodTaggings.Add(p, outList);
}
这是字典方法标签:
private static Dictionary<byte, List<Method>> methodTaggings = new Dictionary<byte, List<Method>>();
现在我总是在行中得到一个NullReferenceException
Console.WriteLine(outList.Count);
但我不明白为什么?即使我在字典中找不到值,列表也不应该为空?
最佳答案
这不应该是:
internal static void removeMethodFromTag(byte p, Method method)
{
Console.WriteLine("Remove method " + method.getName() + " from the tag");
List<Method> outList = new List<Method>();
if (methodTaggings.TryGetValue(p, out outList);
{
Console.WriteLine(outList.Count);
outList.Remove(method);
methodTaggings.Remove(p);
methodTaggings.Add(p, outList);
}
}
关于c# - 找不到这个 NullReferenceException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4745387/