c# - 寻找一种更灵活的方法将定界字符串转换为 StringDictionary

标签 c#

我创建了以下内容以将一些数据公开为正则表达式匹配字符串和 StringDictionary。感觉我可以用更少的行数使用 LINQ 来做到这一点。

private const string STREETTYPES = @"ALY|Alley|AVE|Avenue|BLVD|Boulevard|CIR|Circle|CT|Court|CTR|Center|DR|Drive|EXPY|Expressway|FWY|Freeway|HALL|Hall|HWY|Highway|JCT|Junction|LN|Lane|LP|Loop|PIKE|Pike|PKWY|Parkway|PL|Place|RD|Road|ST|Street|TER|Terrace|TPKE|Turnpike|TRL|Trail|WAY|Way";

  private static StringDictionary streetTypes = null;
  public static StringDictionary StreetTypes
  {
    get
    {
      if (streetTypes != null) return streetTypes;
      streetTypes = new StringDictionary();
      var streetArray = STREETTYPES.Split(PIPE);
      for (int i = 0; i < streetArray.Length-1; i = i+2)
      {
        streetTypes.Add(streetArray[i], streetArray[i + 1]);
      }
      return streetTypes;
    }
  }

最佳答案

怎么样:

private static readonly StringDictionary streetTypes = new StringDictionary
{
    {"ALY","Alley"},{"AVE","Avenue"},{"ALY","Alley"},{"BLVD","Boulevard"},{"CIR","Circle"},
    {"CT","Court"},{"CTR","Center"},{"DR","Drive"},{"EXPY","Expressway"},{"FWY","Freeway"},
    {"HALL","Hall"},{"HWY","Highway"},{"JCT","Junction"},{"LN","Lane"},{"LP","Loop"},
    ...        
};

关于c# - 寻找一种更灵活的方法将定界字符串转换为 StringDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7733208/

相关文章:

C# 设计 : Why is new/override required on abstract methods but not on virtual methods?

C#,使用 zlib 解压缩流

c# - Moq Entity Framework ExecuteSQLCommand

c# - 如何在 LINQ 中随机使用 First()?

c# - 如何检查字符串是否包含字母或数字-正则表达式c#

c# - 如何使用 C# 构建 PathGeometry?

抽象实现接口(interface)上的 C# 协变

c# - 两个应用程序之间的声音交互

c# - aws Lambda 函数中的 Webresponse c#

c# - 当应用程序进程被终止/崩溃时终止子进程