如果國家名單是不可能改變的,你可以做這樣的事情:
// 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
究竟是什麼問題?代碼是行不通的? – bAN 2012-07-13 11:15:37
爲什麼你的人不使用收藏列表 ls =新列表(); ls.add然後循環 –
skhurams
2012-07-13 11:15:46
如果我不想硬編碼這些國家的值,我怎麼能在代碼中處理它? – Vidya 2012-07-13 11:24:43