How to insert a new value in between two values in List<int> with C#

2 Answers

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

namespace ConsoleApplication1
{
    public delegate void mydelegate();

    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            list.Add(1);
            list.Add(3);

            foreach (int n in list)
                Console.WriteLine(n);

            Console.WriteLine();

            int index = list.IndexOf(3);
            list.Insert(index, 2);

            foreach (int n in list)
                Console.WriteLine(n);
        }
    }
}

/*
run:
    
1
3

1
2
3
       
*/

 



answered Apr 22, 2017 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
namespace ConsoleApplication1
{
    public delegate void mydelegate();

    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            list.Add(2);
            list.Add(4);

            foreach (int n in list)
                Console.WriteLine(n);

            Console.WriteLine();

            int index = list.IndexOf(4);
            list.Insert(index, 3);

            foreach (int n in list)
                Console.WriteLine(n);
        }
    }
}

/*
run:
     
2
4

2
3
4
        
*/

 



answered Apr 23, 2017 by avibootz
...