Thuta Learning
C#
IntermediateProgrammingအခြေခံ

Arrays

C#သင်ခန်းစာ 1220 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Arrays ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary နဲ့invalid cases ကိုကိုယ်တိုင်စမ်းနိုင်ရန်
  • Readable, type-safe နဲ့testable C# code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Arrays ကို ရိုးရိုးလေးနားလည်မယ်

Array သည် type တူတဲ့ values များစွာကို တစ်နေရာတည်းမှာသိမ်းတဲ့ fixed-size collection ဖြစ်ပါတယ်။ Array index က 0 မှစတာကြောင့် ပထမဆုံး item ကိုယူချင်ရင် array[0] လို့ရေးရပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

`Length` ကိုနောက်ဆုံး valid index လို့ယူဆခြင်းက `IndexOutOfRangeException` ဖြစ်စေပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Array သည် element type တူပြီးlength သတ်မှတ်ထားသော zero-based collection ဖြစ်ပါတယ်။ Index bounds, reference sharing, multidimensional/jagged shape နဲ့array versus `List<T>` trade-off ကိုသိပါ။

Mental Model
  1. Typed input
  2. C# expression + method
  3. CLR value / object state

Arrays ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

Array သည် element type တူပြီးlength သတ်မှတ်ထားသော zero-based collection ဖြစ်ပါတယ်။ Index bounds, reference sharing, multidimensional/jagged shape နဲ့array versus `List<T>` trade-off ကိုသိပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Typed inputstring[] courses = { "C#", "JavaScript", "PHP", "Java...

    Array သည် type တူတဲ့ values များစွာကို တစ်နေရာတည်းမှာသိမ်းတဲ့ fixed-size collection ဖြစ်ပါတယ်။ Array index က 0 မှစတာကြောင့် ပထမဆုံး item ကိုယူချင်ရင် array[0] လို့ရေးရပါတယ်။

  2. C# expression + methodcourses[1] = "TypeScript";

    Array သည် element type တူပြီးlength သတ်မှတ်ထားသော zero-based collection ဖြစ်ပါတယ်။ Index bounds, reference sharing, multidimensional/jagged shape နဲ့array versus `List<T>` trade-off ကိုသိပါ။

  3. CLR value / object stateConsole.WriteLine($"Total courses: {courses.Length}");

    Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

Execution Flow
  1. Typed input
  2. C# expression + method
  3. CLR value / object state

Success

Arrays ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

`Length` ကိုနောက်ဆုံး valid index လို့ယူဆခြင်းက `IndexOutOfRangeException` ဖြစ်စေပါတယ်။

Array သည် type တူတဲ့ values များစွာကို တစ်နေရာတည်းမှာသိမ်းတဲ့ fixed-size collection ဖြစ်ပါတယ်။ Array index က 0 မှစတာကြောင့် ပထမဆုံး item ကိုယူချင်ရင် array[0] လို့ရေးရပါတယ်။

csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

အရေးကြီးတဲ့အချက်များ

  • courses[0] က ပထမဆုံး item ကိုယူပါတယ်။
  • courses[1] = "TypeScript"; က ဒုတိယ item ကိုပြောင်းပါတယ်။
  • Length က array ထဲမှာ item ဘယ်နှစ်ခုရှိလဲ ပြန်ပေးပါတယ်။
You should see
C# TypeScript Total courses: 4

Info

⚠️ Common mistake

Array မှာ index 0 ကနေစတာမေ့ပြီး နောက်ဆုံး item ကို courses[4] လို့ယူရင် error ဖြစ်ပါတယ်။ ဒီ example မှာ နောက်ဆုံး item က courses[3] ပါ။

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
string[] courses = { "C#", "JavaScript", "PHP", "Java" };

Console.WriteLine(courses[0]);

courses[1] = "TypeScript";
Console.WriteLine(courses[1]);

Console.WriteLine($"Total courses: {courses.Length}");

ရလဒ်: Arrays ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

Quick Quiz 1 / 3

Arrays ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Typed input → C# expression + method → CLR value / object state
  • Array သည် element type တူပြီးlength သတ်မှတ်ထားသော zero-based collection ဖြစ်ပါတယ်။ Index bounds, reference sharing, multidimensional/jagged shape နဲ့array versus `List<T>` trade-off ကိုသိပါ။
  • `Length` ကိုနောက်ဆုံး valid index လို့ယူဆခြင်းက `IndexOutOfRangeException` ဖြစ်စေပါတယ်။
  • Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

C# documentation — Microsoft Learn

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `Length` ကိုနောက်ဆုံး valid index လို့ယူဆခြင်းက `IndexOutOfRangeException` ဖြစ်စေပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Empty array, first/last index, copy versus shared reference နဲ့out-of-range handling ကိုsample တစ်ခုတွင်စမ်းပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: C# TypeScript Total courses: 4

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Arrays | Thuta Learning