Wednesday, August 1, 2018

Sample C# program to get all possible pairs in a list

This is a sample C# program to get possible pairs from a list of integer values. Duplicate and reverse pair values would be discarded in the program.
using System;
using System.Collections.Generic;

namespace PairFromList
{
    class Program
    {
        static void Main(string[] args)
        {
            var items = new List<int>() { 1, 2, 3, 4, 5 };
         
            for (var i = 0; i < items.Count - 1; i++)
            {
                for (var j = i + 1; j < items.Count; j++)
                {
                    Console.WriteLine(items[i] + "-" + items[j]);
                }
            }

            Console.Read();
        }
    }
}
Output :
1-2
1-3
1-4
1-5
2-3
2-4
2-5
3-4
3-5
4-5