gpt4 book ai didi

c# - 找不到这个 NullReferenceException 的原因

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:01 25 4
gpt4 key购买 nike

我有以下方法:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com