如果国家/地区列表不太可能更改,您可以执行以下操作:
// 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 的引用,并在您的使用中包括 System.Configuration):
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