Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 评估一系列条件并在条件满足时停止_C#_Asp.net_Winforms - Fatal编程技术网

C# 评估一系列条件并在条件满足时停止

C# 评估一系列条件并在条件满足时停止,c#,asp.net,winforms,C#,Asp.net,Winforms,这只是一个“最佳实践”问题 我有一个函数,它接受一个输入字符串,然后必须根据内容对其进行更改,但一旦满足特定条件,则所有进一步的处理都将停止 目前,我使用一个“while(true)”循环,然后在得到我想要的东西时使用“break”,下面是伪代码 string Input = "xyz"; string Output = string.Empty; while (true) { if (Input.StartsWith("x")) { Output = "stri

这只是一个“最佳实践”问题

我有一个函数,它接受一个输入字符串,然后必须根据内容对其进行更改,但一旦满足特定条件,则所有进一步的处理都将停止

目前,我使用一个“while(true)”循环,然后在得到我想要的东西时使用“break”,下面是伪代码

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;
}
有没有更“纯粹”的方法来实现上述目标


谢谢

如果这里有其他,您应该使用标准的
。对于您的案例来说,它更为常见和可读

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。您可以创建条件函数和所需输出的
列表
,然后使用
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";
string Input=“xyz”;
字符串输出=string.Empty;
var conditionList=新列表();
添加(Tuple.Create((string x)=>x.StartsWith(“x”),“string以x开头”);
添加(Tuple.Create((字符串x)=>x.Contains(“y”),“字符串中有一个“y”));
条件列表.Add(Tuple.Create((string x)=>x.IndexOf(“z”)==2,“string的第三个字符是“z”));
var firstMatch=conditionList.FirstOrDefault(x=>x.Item1(输入));
输出=第一匹配!=无效的firstMatch.Item2:“字符串不匹配任何条件”;

如果在此处使用其他方法,则应使用标准
。对于您的案例来说,它更为常见和可读

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。您可以创建条件函数和所需输出的
列表
,然后使用
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";
string Input=“xyz”;
字符串输出=string.Empty;
var conditionList=新列表();
添加(Tuple.Create((string x)=>x.StartsWith(“x”),“string以x开头”);
添加(Tuple.Create((字符串x)=>x.Contains(“y”),“字符串中有一个“y”));
条件列表.Add(Tuple.Create((string x)=>x.IndexOf(“z”)==2,“string的第三个字符是“z”));
var firstMatch=conditionList.FirstOrDefault(x=>x.Item1(输入));
输出=第一匹配!=无效的firstMatch.Item2:“字符串不匹配任何条件”;

我认为,
if-else if.
就足够了。如果你重构了你的代码,忘记了最后一个
中断
你可能会面临更大的问题。

我认为,
如果不是这样,如果..
就足够了。如果你重构了你的代码,忘记了最后一个
中断您可能会面临更大的问题。

正如您所说,这只是一个更大问题的小例子,我可能会这样做(当然,对于一个小例子来说,这有些过分,但它的扩展性非常好):

公共接口i条件
{
布尔IsMatch(字符串输入);
字符串GetMessage();
}
公共类启动WithTest:ICondition
{
公共布尔IsMatch(字符串输入)
{
返回输入。用“x”开始;
}   
公共字符串GetMessage()
{
return“字符串以X开头”;
}
}
公共类测试输入
{
私有只读IList_条件;
公共测试输入()
{
_条件=新列表();
_添加(newstartswithtest());
//等等
}
公共字符串测试(字符串输入)
{
var match=_conditions.FirstOrDefault(c=>c.IsMatch(输入));
如果(匹配!=null)
返回match.GetMessage();
其他的
返回字符串。空;
}
}

正如您所说,这只是一个更大问题的小例子,我可能会这样做(当然,对于一个小例子来说,这有些过分,但它的扩展性非常好):

公共接口i条件
{
布尔IsMatch(字符串输入);
字符串GetMessage();
}
公共类启动WithTest:ICondition
{
公共布尔IsMatch(字符串输入)
{
返回输入。用“x”开始;
}   
公共字符串GetMessage()
{
return“字符串以X开头”;
}
}
公共类测试输入
{
私有只读IList_条件;
公共测试输入()
{
_条件=新列表();
_添加(newstartswithtest());
//等等
}
公共字符串测试(字符串输入)
{
var match=_conditions.FirstOrDefault(c=>c.IsMatch(输入));
如果(匹配!=null)
返回match.GetMessage();
其他的
返回字符串。空;
}
}

这样会更好吗?在我过去在CA Clipper中编写代码的好日子里,有一种代码结构,即开始序列…结束序列,您可以在两者之间“中断”。。。在过去的好日子里,当我在CA Clipper中编写代码时,有一个开始序列…结束序列的代码结构,你可以在两者之间“打断”。。。我接受你的说法,但上面的例子只是一个简单的解释。我正在执行的实际过程中包含大量的内容。您可以创建一个
discitionary
,以便为每个键(一个函数,即:
(输入)=>input.Contains(“y”)
)影响适当的输出字符串(
“字符串中有一个“y”
)使用LINQ从字典中读取并返回相应的输出字符串。我接受您的说法,但上面的示例只是一个简单的解释。我正在执行的实际过程中包含大量的内容。您可以创建一个
discitionary
,以便为每个键(一个函数,即:
(输入)=>input.Contains(“y”)
)影响适当的输出字符串(
“字符串中有一个“y”
)使用LINQ从字典中读取并返回相应的输出字符串。我接受您的说法,但上面的示例只是一个简单的解释。我所做的实际过程中有大量的问题,我接受你所说的,但上面的例子只是一个简单的解释。我所做的实际过程中有大量的问题