我有一个客户端程序和一个服务器程序,我目前正在使用哈希表来存储客户端连接时的名称和 IP 地址。我现在需要添加另一个变量,客户端在连接时将发送到服务器,但据我了解,哈希表只有 2 列(键和值)。有没有其他方法可以存储这些数据而不是使用哈希表?
最佳答案
您可以将对象放入哈希表或字典中。
所以让你自己成为一些描述的用户类,然后将你的用户对象以他们的名字存储在你的字典中。
这将有助于在您需要添加其他属性时获取它们。
public class User
{
public string Name { get; set; }
public string IPAddress { get; set; }
public string AnotherProperty { get; set;}
}
Dictionary<string, User> userTable = new Dictionary<string, User>();
userTable.Add(userName, new User(){Name = "fred", IPAddress = "127.0.0.1", AnotherProperty = "blah"});
像这样
关于c# - 存储数据集的最佳方式(在表中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953223/