Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Collection में निर्दिष्ट इंडेक्स पर तत्व डालना

उदाहरण 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
  1. - C# में HashSet में निर्दिष्ट तत्व है या नहीं जांचें

    C# में HashSet किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.Generic; public class Demo { public static void Main() { HashSet set1 = new HashSet(); se

  1. - C# में HashSet और निर्दिष्ट संग्रह में सामान्य तत्व हैं या नहीं जांचें

    C# में एक HashSet और एक निर्दिष्ट संग्रह में सामान्य तत्व हैं या नहीं, यह जांचने के लिए Overlaps() मेथड का उपयोग किया जाता है। यह मेथड true लौटाता है यदि दोनों संग्रहों में कम से कम एक सामान्य तत्व है। उदाहरण 1 using System; using System.Collections.Generic; public class Demo { public static vo

  1. - C# में इंडेक्स का उपयोग करके लिस्ट में आइटम इंसर्ट करना

    Insert() मेथड से इंडेक्स पर आइटम जोड़ना लिस्ट सेट करें: List<int> list = new List<int>(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459); इंडेक्स 5 पर आइटम जोड़ें: list.Insert(5, 999); पूर्ण कोड उदाहरण using System; using System.Collections.