उदाहरण 1: Collection में Insert मेथड का उपयोग
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection col = new Collection();
col.Add("Laptop");
col.Add("Desktop");
col.Add("Notebook");
col.Add("Ultrabook");
col.Add("Tablet");
col.Add("Headphone");
col.Add("Speaker");
Console.WriteLine("Collection में तत्व...");
foreach(string str in col) {
Console.WriteLine(str);
}
Console.WriteLine("इंडेक्स 3 पर तत्व = " + col[3]);
Console.WriteLine("इंडेक्स 4 पर तत्व = " + col[4]);
col.Insert(5, "Alienware");
Console.WriteLine("Collection में तत्व... अपडेटेड");
foreach(string str in col) {
Console.WriteLine(str);
}
}
}
आउटपुट
Collection में तत्व...
Laptop
Desktop
Notebook
Ultrabook
Tablet
Headphone
Speaker
इंडेक्स 3 पर तत्व = Ultrabook
इंडेक्स 4 पर तत्व = Tablet
Collection में तत्व... अपडेटेड
Laptop
Desktop
Notebook
Ultrabook
Tablet
Alienware
Headphone
Speaker
उदाहरण 2: डुप्लिकेट तत्वों के साथ Collection में Insert
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection col = new Collection();
col.Add("Andy");
col.Add("Kevin");
col.Add("John");
col.Add("Kevin");
col.Add("Mary");
col.Add("Katie");
col.Add("Barry");
col.Add("Nathan");
Console.WriteLine("Collection में तत्व...");
foreach(string str in col) {
Console.WriteLine(str);
}
col.Insert(3, "Jacob");
Console.WriteLine("Collection में तत्व... अपडेटेड");
foreach(string str in col) {
Console.WriteLine(str);
}
}
}
आउटपुट
Collection में तत्व...
Andy
Kevin
John
Kevin
Mary
Katie
Barry
Nathan
Collection में तत्व... अपडेटेड
Andy
Kevin
John
Jacob
Kevin
Mary
Katie
Barry
Nathan