← Back to C# Course | Chapter 9: Collections | Lesson 2 of 7

List<T> क्या है

एक List एक लचीला array है जो चीज़ें जोड़ने या हटाने पर बढ़ता और सिकुड़ता है। यह एक backpack जैसा है जो ज़्यादा किताबें रखने के लिए फैलता है।

In this page:

  1. List<T>
Syntax
csharp
List<type> listName = new List<type>();
listName.Add(item);
listName.Remove(item);
listName[0];
listName.Count;

List<T>

List<T> System.Collections.Generic से एक dynamically resizable collection है, fixed-size arrays के विपरीत। आप elements जोड़, हटा, और search कर सकते हैं, और यह ज़रूरत पड़ने पर अपने आप बढ़ता है। C# में यह सबसे आम तौर पर उपयोग होने वाली general-purpose collection है।

Note: जब भी runtime पर elements की संख्या बदल सकती हो arrays से List<T> को प्राथमिकता दें।

उदाहरण: List<T>

markup
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> names = new List<string> { "Amy", "Bo" };
        names.Add("Cy");
        names.Remove("Bo");

        Console.WriteLine(names.Count);
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी foreach के अंदर किसी list से items हटाना, जो InvalidOperationException फेंकता है।
  2. Count के बराबर एक index उपयोग करना, जो ArgumentOutOfRangeException फेंकता है।
  3. implicit usings disabled होने पर using System.Collections.Generic; भूल जाना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.