तुम सिर्फ switch/case
का उपयोग करना चाहते हैं, तो आप कुछ इस तरह कर सकते हैं, छद्म कोड:
string message = "test of mine";
string[] keys = new string[] {"test2", "test" };
string sKeyResult = keys.FirstOrDefault<string>(s=>message.Contains(s));
switch (sKeyResult)
{
case "test":
Console.WriteLine("yes for test");
break;
case "test2":
Console.WriteLine("yes for test2");
break;
}
लेकिन अगर कुंजी की मात्रा एक बड़ा है, तो आप सिर्फ यह शब्दकोश के साथ, जगह ले सकता है इस तरह :
static Dictionary<string, string> dict = new Dictionary<string, string>();
static void Main(string[] args)
{
string message = "test of mine";
// this happens only once, during initialization, this is just sample code
dict.Add("test", "yes");
dict.Add("test2", "yes2");
string sKeyResult = dict.Keys.FirstOrDefault<string>(s=>message.Contains(s));
Console.WriteLine(dict[sKeyResult]); //or `TryGetValue`...
}
धन्यवाद! यही वह था जो मुझे – pmerino