Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Collections (List)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

List<T> သည် size ပြောင်းနိုင်တဲ့ collection ဖြစ်ပါတယ်။ Array က fixed-size ဖြစ်ပေမယ့် List မှာ item ထပ်ထည့်တာ၊ ဖျက်တာ၊ ရှာတာတွေ ပိုလွယ်ပါတယ်။ Real project တွေမှာ items မည်မျှရှိမလဲ မသိတဲ့အခါ List ကို အများကြီးသုံးပါတယ်။

csharp
using System;
using System.Collections.Generic;

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

        tasks.Add("Learn C# basics");
        tasks.Add("Practice methods");
        tasks.Add("Build a mini project");

        tasks.Remove("Practice methods");

        foreach (string task in tasks)
        {
            Console.WriteLine(task);
        }
    }
}

ဒီ code မှာ သင်ယူရမယ့်အချက်

  • using System.Collections.Generic; ထည့်မှ List<T> သုံးနိုင်ပါတယ်။
  • Add() က item ထည့်ပါတယ်။
  • Remove() က item ဖျက်ပါတယ်။
  • foreach နဲ့ list ထဲက item တိုင်းကို loop လုပ်ပါတယ်။
You should see
Learn C# basics Build a mini project
Collections (List) | Thuta Learning