C# - 字典 <String, Action> 方法 'Add' 没有重载需要 1 个参数

C# - Dictionary<String, Action> No overload for method 'Add' takes 1 arguments

嗯,我想在我的项目中做点什么。它是基于网络的,我正在使用字典来处理我的数据包前缀。

No overload for method 'Add' takes 1 arguments Here's my code anyway:

    private Dictionary<string, Action> worldPackets = new Dictionary<string, Action>
    {
        "jr", User.HandleJoinRoom,
        "sm", User.HandleSendMessage,
        "cr", User.HandleCreateRoom
    };

谢谢。

你需要用花括号来分隔每一对

private Dictionary<string, Action> worldPackets = new Dictionary<string, Action>
{
    { "jr", User.HandleJoinRoom },
    { "sm", User.HandleSendMessage },
    { "cr", User.HandleCreateRoom }
};