国のリストが変更される可能性が低い場合は、次のようにすることができます。
// 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 への参照を含め、Usings に 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