IF语句:确定所选内容。C#

IF语句:确定所选内容。C#,c#,boolean,C#,Boolean,如果使用|,我需要确定if语句中哪个条件为真 例如: if(trueone() || truetwo() || truethree()) { if([magical code] == trueOne()) { // ...do my code here... } } 如果trueone()为真,则说明选择了“trueone” 或者,如果trueone()和truetwo()为真,则说明选择了“trueone和truetwo” 编辑:请不要切换。 编辑2: 这里有更多的细

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

例如:

if(trueone() || truetwo() || truethree()) {
   if([magical code] == trueOne()) {
      // ...do my code here...
   }
}
如果
trueone()
为真,则说明
选择了“trueone”

或者,如果
trueone()
truetwo()
为真,则说明
选择了
“trueone和truetwo”

编辑:请不要切换。
编辑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");
}

任何能更有效地完成相同任务的方法都是有用的。

如果这是原始代码:

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
放在一个数组中

喜欢

现在,如果要打印行号而不是行号,可以a.-使用计数器,b.-使用a
for
而不是第一个
foreach

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)));
        }
    }
}
for(int i=0;i
I:

如果要检查的内容列表很长。。。使其成为实际列表(例如
列表

var allConditions=新列表{trueone,truetwo,truethree};
var trueConditions=allConditions.Where(p=>p!=null&&p()).ToArray();
如果(trueConditions.Length>0)
{
if(trueConditions.Contains(trueone))
{
//…在这里执行我的代码。。。
}
}
并在其上循环:

var allConditions=新列表{trueone,truetwo,truethree};
var trueConditions=allConditions.Where(p=>p!=null&&p()).ToArray();
如果(trueConditions.Length>0)
{
foreach(所有条件下的var条件)
{
if(trueConditions.Contains(条件))
{
//…在这里执行我的代码。。。
}
}
}
那是在你说要检查之前

如果行包含某个关键字

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

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=“此处要搜索的字符串”;
var allConditions=newlist{()=>x.Contains(“关键字”),()=>x.Contains(“to”),()=>x.Contains(“查找”);
var trueConditions=allConditions.Where(p=>p!=null&&p()).ToArray();
如果(trueConditions.Length>0)
{
foreach(所有条件下的var条件)
{
if(trueConditions.Contains(条件))
{
//…在这里执行我的代码。。。
}
}
}
但是,由于您只需要检查字符串,因此可以创建一个列表:

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...
        }
    }
}
var x=“此处要搜索的字符串”;
var allKeywords=新列表{“关键字”、“收件人”、“查找”};
var foundKeywords=allKeywords.Where(s=>x.Contains(s)).ToArray();
如果(foundKeywords.Length>0)
{
foreach(所有关键字中的var关键字)
{
if(foundKeywords.Contains(关键字))
{
//…在这里执行我的代码。。。
}
}
}

if
/
else if
构造可以直接使用时,为什么需要使用
|
?甚至可能是一个
开关
案例。在我的特定程序中,对每一个真/假语句执行
if
和类似操作都会使它变得非常长和丑陋。这是一个家庭作业吗???你可能想用这些信息更新你的问题,它是相当广泛的。好吧,,为什么不为正在查找的字符串使用另一个数组/列表?
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...
        }
    }
}