2012-07-13 33 views
1
private bool CheckMemberCountry(string country) 
{ 
    string[] countries = new string[] { "AF", "BD", "CA", "IN", "IR", "RO", "AN", "CY", "IL", "PH" }; 
    foreach (string memberCountry in countries) 
    { 
     if (memberCountry.Equals(country)) 
     { 
      return true; 
     } 
    } 

    return false; 
} 

我不想硬编码像上面的值,我怎么能处理它更好的方式来处理下面的代码

+0

究竟是什么问题?代码是行不通的? – bAN 2012-07-13 11:15:37

+0

为什么你的人不使用收藏列表 ls =新列表(); ls.add然后循环 – skhurams 2012-07-13 11:15:46

+0

如果我不想硬编码这些国家的值,我怎么能在代码中处理它? – Vidya 2012-07-13 11:24:43

回答

3

使用String.Contains()

static string[] Countries = new string[] { "AF", "BD", "CA", "IN", "IR", "RO", "AN", "CY", "IL", "PH" };  

private bool CheckMemberCountry(string country) 
{  
    return Countries.Contains(country); 
} 
+0

不错,简单,但我会推荐国家阵列被宣布为静态全局 – musefan 2012-07-13 11:19:10

+0

@musefan干杯,编辑我的帖子 – Curt 2012-07-13 11:22:46

+0

如果我不想硬编码这些国家的值,我怎么能在代码中处理它? – Vidya 2012-07-13 11:24:02

4

最短的方法是重新 - 写入它作为一条线,但它是不是最有效的:

return (new string[] { "AF", "BD", "CA", "IN", "IR", "RO", "AN", "CY", "IL", "PH" }) 
    .Contains(country); 

你应该把数组静态只读变量,和u本质上它在你的函数:

private static readonly string[] AllCountries = new string[] { 
    "AF", "BD", "CA", "IN", "IR", "RO", "AN", "CY", "IL", "PH" 
}; 

private bool CheckMemberCountry(string country) { 
    return AllCountries.Contains(country); 
} 
+0

如果我不想硬编码这些国家的值,我怎么能在代码中处理它? – Vidya 2012-07-13 11:25:03

+0

@Vidya如果你不想对它们进行硬编码,你可以从非硬编码值应该来自的任何源分配'AllCountries'。做这件事最好的地方是你的包含类的静态构造函数。 – dasblinkenlight 2012-07-13 11:28:08

0
private bool CheckMemberCountry(string country) 
{ 
    return new string[] { "AF", "BD", "CA", "IN", "IR", "RO", "AN", "CY", "IL", "PH" }.Contains(country); 
} 
1

如果国家名单是不可能改变的,你可以做这样的事情:

// note: sorted alphabetically 
private static readonly string[] countries = new string[] { 
    "AF", "AN", "BD", "CA", "CY", "IL", 
    "IN", "IR", "PH", "RO" }; 

private bool CheckMemberCountry(string country) 
{ 
    return Array.BinarySearch<string>(countries, country) >= 0; 
} 

如果国家做出改变,你可能想把它们放在一个配置文件中。您的App.config文件可能类似于:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="countries" value="AF,BD,CA,IN,IR,RO,AN,CY,IL,PH"/> 
    </appSettings> 
</configuration> 

而在上面的代码中,你可以更换行:

private static readonly string[] countries = new string[] { 
     "AF", "AN", "BD", "CA", "CY", "IL", 
     "IN", "IR", "PH", "RO" }; 

用(包括对System.Configuration.dll的引用,包括系统。配置在你的使用):

using System.Configuration; 

// ... 

private static readonly string[] countries = ConfigurationManager 
    .AppSettings["countries"] // configuration setting with key "countries" 
    .Split(',') // split comma-delimited values 
    .Select(a=>a.Trim()) // trim each value (remove whitespace) 
    .OrderBy(a=>a) // sort list (for binary search) 
    .ToArray(); // convert to array 
0

这是一个很好的做法,以尽可能避免代码中的硬编码字符串。试试这个 -

public enum Country 
{ 
    AF, BD, CA, IN, IR, RO, AN, CY, IL, PH 
} 

...那么,

foreach (string name in Enum.GetNames(typeof(Country))) 
{ 
    //ToDo 
} 
+0

和硬编码的字符串和硬编码的枚举之间的区别是什么? – 2012-07-13 12:07:20

+0

@FrancescoBaruchelli:这将是“少排印” – atiyar 2012-07-13 12:09:35

+0

对于OP,允许国家名称不包含在列表中(看循环中的测试),所以使用枚举是无用的,它是仍然是硬编码 – 2012-07-13 12:54:01

相关问题