C#通过字典查找对象的特定条件

The*_*eAJ 0 c#

当前我正在使用此方法来确定角色是否在线:

    public bool OnlineByServer(string username)
    {
        foreach (Character c in this.characters.Values)
        {
            if (c != null && c.Username.Equals(username))
            {
                return true;
            }
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

有更快的方法吗?

Rex*_*x M 9

最快的方法是使用户名成为字典的关键.