if ((message.Contains("test")))
{
Console.WriteLine("yes");
} else if ((message.Contains("test2"))) {
Console.WriteLine("yes for test2");
}
会有任何方式改变switch()
的if()
陈述的C#应用程序?
if ((message.Contains("test")))
{
Console.WriteLine("yes");
} else if ((message.Contains("test2"))) {
Console.WriteLine("yes for test2");
}
会有任何方式改变switch()
的if()
陈述的C#应用程序?
您可以先检查一下,然后根据需要使用该开关。
例如:
string str = "parameter"; // test1..test2..test3....
if (!message.Contains(str)) return ;
然后
switch(str)
{
case "test1" : {} break;
case "test2" : {} break;
default : {} break;
}
如果你只是想用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`...
}
一些定制马桶盖可以这样创建。允许多个情况下执行以及
public class ContainsSwitch
{
List<ContainsSwitch> actionList = new List<ContainsSwitch>();
public string Value { get; set; }
public Action Action { get; set; }
public bool SingleCaseExecution { get; set; }
public void Perform(string target)
{
foreach (ContainsSwitch act in actionList)
{
if (target.Contains(act.Value))
{
act.Action();
if(SingleCaseExecution)
break;
}
}
}
public void AddCase(string value, Action act)
{
actionList.Add(new ContainsSwitch() { Action = act, Value = value });
}
}
这样调用
string m = "abc";
ContainsSwitch switchAction = new ContainsSwitch();
switchAction.SingleCaseExecution = true;
switchAction.AddCase("a", delegate() { Console.WriteLine("matched a"); });
switchAction.AddCase("d", delegate() { Console.WriteLine("matched d"); });
switchAction.AddCase("a", delegate() { Console.WriteLine("matched a"); });
switchAction.Perform(m);
这将工作在C#7在撰写本文时,它尚未公布。但是如果我正确理解这个,这段代码就可以工作。
switch(message)
{
case Contains("test"):
Console.WriteLine("yes");
break;
case Contains("test2"):
Console.WriteLine("yes for test2");
break;
default:
Console.WriteLine("No matches found!");
}
资料来源:[先生https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
不,这是行不通的。'包含在当前上下文中不存在.' – user9993
@ user9993你用C#7试过这个吗? –
正确的最终语法C]的答案。
随着VS2017RC的释放,其C#7支持它的工作原理是这样的:
switch(message)
{
case string a when a.Contains("test2"): return "no";
case string b when b.Contains("test"): return "yes";
}
你应该照顾的情况下订购的第一场比赛将有所回升。这就是为什么在测试之前放置“test2”的原因。
确定环境时,这个问题面前,我想出了一个如下班轮:
string ActiveEnvironment = localEnv.Contains("LIVE") ? "LIVE" : (localEnv.Contains("TEST") ? "TEST" : (localEnv.Contains("LOCAL") ? "LOCAL" : null));
这样一来,如果找不到提供的字符串相匹配的“开关”,在任何条件,它放弃并返回null
。这可以很容易地修改为返回一个不同的值。
这不是严格的一个开关,更多的级联if语句,但它是整洁,它的工作。
谢谢!这是我需要的 – pmerino