2012-07-13 41 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

相關問題