IF声明:确定所选内容。 C#

时间:2017-11-12 02:03:46

标签: c# boolean

如果使用||,我需要确定if语句中哪个条件为TRUE。

示例:

if(trueone() || truetwo() || truethree()) {
   if([magical code] == trueOne()) {
      // ...do my code here...
   }
}

如果trueone()成立,则说出"trueOne was selected"
或者,如果trueone()truetwo()为真,则说"trueOne and trueTwo were selected"

编辑:请不要开关。
编辑2

更多细节: 该程序旨在使用foreach语句查看文件及其行。如果该行包含某个关键字,则将其打印给用户。 目前,该计划如下:

foreach(string x in lines) {
   if(x.Contains("stringtofind")) {
      Console.WriteLine("Found stringtofind at line x");
   if(x.Contains("stringtofind2")) {
      Console.WriteLine("Found stringtofind2 at line x");
}

任何可以完成相同任务的更高效的方法都是有用的。

2 个答案:

答案 0 :(得分:1)

如果这是原始代码:

foreach(string x in lines) {
   if(x.Contains("stringtofind")) {
      Console.WriteLine("Found stringtofind at line x");
   if(x.Contains("stringtofind2")) {
      Console.WriteLine("Found stringtofind2 at line x");
   ...
}

我们可以看到foreach循环中有一个模式 为了删除重复的代码,我们可以将所有stringsToFind放在一个数组中。

像;

var lines = new string[]
{
    "line1  stringToFind1 stringToFind2",
    "line2 ",
    "line3 stringToFind3",
    "line4 stringToFind4 stringToFind5",
};

var stringsToFind = new string[]
{
    "stringToFind1",
    "stringToFind2",
    "stringToFind3",
    "stringToFind4",
    "stringToFind5",
};

foreach (string line in lines)
{
    foreach (string stringToFind in stringsToFind)
    {
        if (line.Contains(stringToFind))
        {
            Console.WriteLine(string.Format("Found {0} at line {1}", stringToFind, line));
        }
    }
}

现在,如果你想打印行号而不是行号,你可以a.-使用计数器,b .-使用for代替第一个foreach。 / p>

for (int i = 0; i < lines.Length; i++)
{
    foreach (string stringToFind in stringsToFind)
    {
        if (lines[i].Contains(stringToFind))
        {
            // We use i+1 for line number to show that in a 'human' format.
            Console.WriteLine(string.Format("Found {0} at line {1}", stringToFind, (i+1)));
        }
    }
}

答案 1 :(得分:0)

was saying

  

如果要检查的事项列表很长...将其设为实际列表(例如List<Func<bool>>

var allConditions = new List<Func<bool>> { trueone, truetwo, truethree };
var trueConditions = allConditions.Where(p => p != null && p()).ToArray();
if (trueConditions.Length > 0)
{
    if (trueConditions.Contains(trueone))
    {
        // ...do my code here...
    }
}
  

并循环播放:

var allConditions = new List<Func<bool>> { trueone, truetwo, truethree };
var trueConditions = allConditions.Where(p => p != null && p()).ToArray();
if (trueConditions.Length > 0)
{
    foreach (var condition in allConditions)
    {
        if (trueConditions.Contains(condition))
        {
            // ...do my code here...
        }
    }
}

现在,那是在你说你想检查之前......

  

如果该行包含某个关键字

您可以在上面的框架上执行此操作:

var x = "some string here to search in";
var allConditions = new List<Func<bool>> { () => x.Contains("keywords"), () => x.Contains("to"), () => x.Contains("find") };
var trueConditions = allConditions.Where(p => p != null && p()).ToArray();
if (trueConditions.Length > 0)
{
    foreach (var condition in allConditions)
    {
        if (trueConditions.Contains(condition))
        {
            // ...do my code here...
        }
    }
}

然而,由于您只需要检查字符串,您可以改为列出这些字符串:

var x = "some string here to search in";
var allKeywords = new List<string> { "keywords", "to", "find" };
var foundKeywords = allKeywords.Where(s => x.Contains(s)).ToArray();
if (foundKeywords.Length > 0)
{
    foreach (var keyword in allKeywords)
    {
        if (foundKeywords.Contains(keyword))
        {
            // ...do my code here...
        }
    }
}