C#通过字典查找对象的特定条件(C# finding a certain condition of a object via a dictionary)
当前我使用此方法来确定角色是否在线:
public bool OnlineByServer(string username) { foreach (Character c in this.characters.Values) { if (c != null && c.Username.Equals(username)) { return true; } } return false; }有更快的方法吗?
Current i am using this method to determine if a character is online:
public bool OnlineByServer(string username) { foreach (Character c in this.characters.Values) { if (c != null && c.Username.Equals(username)) { return true; } } return false; }Is there a faster way to do this?
最满意答案
如果要将字符保留为字典值,则实际上没有更快的方法。 由于未排序,必须进行线性O(n)搜索。
There isn't really a faster way of doing it, if you want to keep the characters as the dictionary Values. Due to being unsorted, a linear O(n) search has to be done.
C#通过字典查找对象的特定条件(C# finding a certain condition of a object via a dictionary)当前我使用此方法来确定角色是否在线:
public bool OnlineByServer(string username) { foreach (Character c in this.characters.Values) { if (c != null && c.Username.Equals(username)) { return true; } } return false; }有更快的方法吗?
Current i am using this method to determine if a character is online:
public bool OnlineByServer(string username) { foreach (Character c in this.characters.Values) { if (c != null && c.Username.Equals(username)) { return true; } } return false; }Is there a faster way to do this?
最满意答案
如果要将字符保留为字典值,则实际上没有更快的方法。 由于未排序,必须进行线性O(n)搜索。
There isn't really a faster way of doing it, if you want to keep the characters as the dictionary Values. Due to being unsorted, a linear O(n) search has to be done.
发布评论