How to check is a list contain a number in C#

2 Answers

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
                bool a = list.Contains(10);
                bool b = list.Contains(20);

                Console.WriteLine(a);
                Console.WriteLine(b);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
 
True
False
 
*/


answered Apr 4, 2015 by avibootz
edited Apr 4, 2015 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int n = 10;
                var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

                if (list.Contains(n))
                    Console.WriteLine("{0} in the list", n);

                n = 50;
                if (list.Contains(n))
                    Console.WriteLine("{0} in the list", n);
                else
                    Console.WriteLine("{0} not in the list", n);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
   
10 in the list
50 not in the list
 
*/


answered Apr 4, 2015 by avibootz
edited Apr 4, 2015 by avibootz

Related questions

1 answer 294 views
1 answer 121 views
1 answer 118 views
1 answer 134 views
...