2013-04-23 31 views
0

这仅仅是一个“最佳实践”的问题...评估一系列条件和停止条件时遇到

我有一个功能,其采用输入字符串,然后具有基于内容,但一旦改变它满足特定的条件,则所有进一步处理停止。

目前,我使用的是“而(真)”循环,然后“破发”时,我已经得到了我想要的东西,下面是伪代码..

string Input = "xyz"; 
string Output = string.Empty; 
while (true) 
{ 
    if (Input.StartsWith("x")) 
    { 
     Output = "string starts with an X"; 
     break; 
    } 

    if (Input.Contains("y")) 
    { 
     Output = "string has a 'y' in it"; 
     break; 
    } 

    if (Input.IndexOf("z") == 2) 
    { 
     Output = "string has a 'z' as the 3rd character"; 
     break; 
    } 

    Output = "string does not match any conditions"; 
    break; 
} 

有没有更“纯粹主义”的方式达到上述目的?

感谢

+0

[CodeReview.SE]会更好吗? – 2013-04-23 08:12:52

+0

在我以前用CA-Clipper编码的美好时代,有BEGIN SEQUENCE .... END SEQUENCE的代码构造,你可以在...之间“打破”... http://www.itlnet .net/programming/program/reference/c53g01c/ngfc7b7.html – 2013-04-23 09:21:18

回答

4

你应该在这里使用标准if-ifelse-else。这对你的情况更为常见和可读。

string Input = "xyz"; 
string Output = string.Empty; 

if (Input.StartsWith("x")) 
{ 
    Output = "string starts with an X"; 
} 
else if (Input.Contains("y")) 
{ 
    Output = "string has a 'y' in it"; 
} 
else if (Input.IndexOf("z") == 2) 
{ 
    Output = "string has a 'z' as the 3rd character"; 
} 
else 
{ 
    Output = "string does not match any conditions"; 
} 

更新

第二个版本,使用LINQ。您可以创建条件函数和所需输出的List,然后使用FirstOrDefault获得第一个匹配条件。

string Input = "xyz"; 
string Output = string.Empty; 

var conditionList = new List<Tuple<Func<string, bool>, string>>(); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.StartsWith("x"), "string starts with x")); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.Contains("y"), "string has a 'y' in it")); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.IndexOf("z") == 2, "string has a 'z' as the 3rd character")); 

var firstMatch = conditionList.FirstOrDefault(x => x.Item1(Input)); 
Output = firstMatch != null ? firstMatch.Item2 : "string does not match any conditions"; 
+0

我接受你在说什么,但上面的例子只是一个简单的解释。我正在做的实际过程有相当数量的 – 2013-04-23 08:24:52

0

我认为if else if..应该就足够了。你做到这一点,如果你重构你的代码,忘记最后的break;你可能会面临更大的问题。

+0

我接受你在说什么,但上面的例子只是一个简单的解释。 我正在做的实际过程有相当数量。 – 2013-04-23 08:16:20

+0

您可以为每个键(一个函数即:((input)=> input.Contains(“y”))创建一个'Disctionary ,string>'影响相应的输出字符串'“字符串中有'y')'')使用LINQ从字典中读取并返回适当的输出字符串。 – 2013-04-23 08:27:03

1

正如你所说,这是一个更大的问题的只是一个简单的例子,我可能会做这样的事情(当然这是矫枉过正的一个小例子,但它做的非常好):

public interface ICondition 
{ 
    bool IsMatch(string input); 
    string GetMessage(); 
} 

public class StartsWithTest : ICondition 
{ 
    public bool IsMatch(string input) 
    { 
     return input.StartsWith("x"); 
    } 

    public string GetMessage() 
    { 
     return "string starts with an X"; 
    } 
} 


public class TestInput 
{ 

    private readonly IList<ICondition> _conditions; 

    public TestInput() 
    { 
     _conditions = new List<ICondition>(); 

     _conditions.Add(new StartsWithTest()); 
     //etc etc 
    } 

    public string Test(string input) 
    { 
     var match = _conditions.FirstOrDefault(c => c.IsMatch(input)); 

     if (match != null) 
      return match.GetMessage(); 
     else 
      return string.Empty; 
    } 
} 
+0

优雅的解决方案! – 2013-04-23 08:40:51