List<string>以外的字符串存储方法

Method of storing strings other than List<string>

我正在开发一个扑克应用程序,目前我想存储我计划使用的所有纸牌组合名称列表并执行如下操作:

private static List<string> combinationNames = new List<string>
{
    " High Card ",
    " Pair ",
    " Two Pair ",
    " Three of a Kind ",
    " Straight ",
    " Flush ",
    " Full House ",
    " Four of a Kind ",
    " Straight Flush ",
    " Royal Flush ! "
};

for (int j = 0; j < combinationNames.Count; j++)
{
    if (current == j)
    {
        MessageBox.Show("You Have : ", combinationNames[j]);

    }
}

那么有没有更好的方法来存储这些名称并稍后像我一样访问它们?

在您的问题中没有太多内容可以了解您的代码具体有什么问题。也就是说,至少我希望以下内容有所改进:

private readonly static string[] combinationNames =
{
    " High Card ",
    " Pair ",
    " Two Pair ",
    " Three of a Kind ",
    " Straight ",
    " Flush ",
    " Full House ",
    " Four of a Kind ",
    " Straight Flush ",
    " Royal Flush ! "
};

if (current >= 0 && current < combinationNames.Length)
{
    MessageBox.Show("You Have : ", combinationNames[current]);
}

即:

  • 由于列表不会改变,所以可以用数组代替列表
  • 由于列表对象不会改变,变量可以是readonly
  • 您对 j 所做的所有代码都是将其与 current 进行比较; j 不需要枚举所有可能的值……只要确保 current 在有效范围内,然后直接使用它的值即可。

请注意最后一点,您从哪里得到 current 并不是很清楚,但在您显示文本之前应该已经保证它是有效的,所以您甚至不应该真的需要范围检查。我只是把它放在那里以确保上面的新版本代码与您显示的代码的行为合理一致(那里很少)。

如果您需要比上述更具体的建议,请更准确地解释您的想法 "better" 以及您现在拥有的代码在哪些方面不能充分满足您的需求。 IE。代码现在做了什么,它与您想要的有何不同?