使用列表读取bool值

时间:2015-10-29 10:06:37

标签: c#

请考虑以下代码:

public bool IsTwoInductions()
{
   List<Induction> inductionList = GetInduction();
   int? introTime = 0;
   foreach (var items in inductionList)
   {
     introTime = items.TotalTime;
   }
   if(introTime == 90)
   {
      return true;
   }
   else
   {
      return false;
   }
}

我的列表包含两条TotalTime == 90TotalTime == 120的记录。现在,当我将值输出到控制台时,我得到了

  

     

但它应该是TrueFalse ..我哪里错了?

3 个答案:

答案 0 :(得分:2)

public void IsTwoInductions()
{
   List<Induction> inductionList = GetInduction();
   int? introTime = 0;
   foreach (var items in inductionList)
   {
     introTime = items.TotalTime;

     if(introTime == 90)
     {
        Console.Write("true ");
     }
     else
     {
        Console.Write("false ");
     }
   }
}

答案 1 :(得分:1)

如果我理解正确:

private void SomeMethod()
{
   List<int> inductionList = new List<int>() { 90, 120};
   int? introTime = 0;
   bool isEquelToNinety = false;
   foreach (var items in inductionList)
   {
      Console.WriteLine(isEquelToNinety=IsOK(items));
   }            
}

private bool IsOK(int? introTime)
{            
   if (introTime == 90)
   {
      return true;
   }
   else
   {
      return false;
   }
}

答案 2 :(得分:0)

你试过这个吗?

public bool IsTwoInductions()
{
   List<Induction> inductionList = GetInduction();
   bool isHaving90 = false;
   foreach (var items in inductionList)
   {
     introTime = items.TotalTime;
     if(introTime == 90)
     {
        isHaving90 = true;
        break;
     }
   }
   return isHaving90;
}