पहले से बनाई गई सूची में किसी आइटम को सम्मिलित करने के लिए, सम्मिलित करें () विधि का उपयोग करें।
सबसे पहले, तत्वों को सेट करें -
List <int> list = new List<int>(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909);
अब, मान लें कि आपको चौथे स्थान पर एक आइटम डालने की आवश्यकता है। उसके लिए, सम्मिलित करें () विधि का उपयोग करें -
// inserting element at 4th position list.Insert(3, 567);
आइए देखें पूरा उदाहरण -
उदाहरण
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
List < int > list = new List < int > ();
list.Add(989);
list.Add(345);
list.Add(654);
list.Add(876);
list.Add(234);
list.Add(909);
Console.WriteLine("Count: {0}", list.Count);
Console.Write("List: ");
foreach(int i in list) {
Console.Write(i + " ");
}
// inserting element at 4th position
list.Insert(3, 567);
Console.Write("\nList after inserting a new element: ");
foreach(int i in list) {
Console.Write(i + " ");
}
Console.WriteLine("\nCount: {0}", list.Count);
}
}
}