我想检查项目是否在数组/列表中,如果是,那么如果没有那么写下来然后告诉他们它不在那里

时间:2017-06-10 13:54:53

标签: c#

所以,如果我开始我的代码一切都很好,但是当我到达它要求选择附件的部分时,我写下列表中的那个,但它只是说它不在那里我在做什么wronk ?

  Console.WriteLine("Do you want any attachments on your gun?");
  if(Console.ReadLine() == "Yes")
  {
         attachments.ToList().ForEach(i=> Console.WriteLine(i.ToString()));
         Console.WriteLine("Choose A Attachment" + "\n");
         string q = Console.ReadLine();
         if (q != attachments.ToList().ToString())
         {
              Console.WriteLine("not found");
         }
         else if (q == attachments.ToList().ToString())
         {
             Console.WriteLine("attachment added");       
         }


  }

1 个答案:

答案 0 :(得分:0)

if (q != attachments.ToList().ToString())

应该是:

if (!attachments.Contains(q)) // You may need a .ToList() before .Contains

else if (q == attachments.ToList().ToString())

应该是:

else