How to search for an int element in a List with C#

3 Answers

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>(new int[] { 18, 21, 31, 12, 7 });

            int index = list.IndexOf(31); 
            Console.WriteLine(index);

            index = list.IndexOf(16); 
            Console.WriteLine(index);
        }
    }
}


/*
run:
      
2
-1

*/

 



answered Dec 26, 2016 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>(new int[] { 18, 21, 31, 12, 7 });

            if (list.IndexOf(31) != -1) 
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");
        }
    }
}


/*
run:
      
found

*/

 



answered Dec 26, 2016 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>(new int[] { 18, 21, 31, 12, 7 });

            foreach (int n in list)
            {
                if (n == 31) 
                {
                    Console.WriteLine("found");
                }
            }
        }
    }
}


/*
run:
      
found

*/

 



answered Dec 26, 2016 by avibootz
...