Thuta Learning
C#
IntermediateProgrammingအခြေခံ

Properties

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

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

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

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

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

Properties များသည် private data ကို လုံခြုံစွာ access လုပ်ရန်အသုံးပြုပါတယ်။ Direct field ကို public ပေးမထားဘဲ get / set မှတစ်ဆင့် ထိန်းချုပ်နိုင်တာကြောင့် validation logic ထည့်ဖို့လည်း အဆင်ပြေပါတယ်။

Problem → Why → Solution

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

Auto-property အားလုံးကိုpublic setter ပေးခြင်းကencapsulation ကိုမဖြစ်စေပါ။

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

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

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

Properties ကfield-like syntax နဲ့controlled access ပေးပြီး validation, computed values, `init` နဲ့required initialization ကိုဖော်ပြနိုင်ပါတယ်။ Getter တွင်hidden expensive work/side effects မထားဘဲ nullability နဲ့mutability ကိုAPI contract အဖြစ်သတ်မှတ်ပါ။

Mental Model
  1. Object / dependency
  2. C# type contract + CLR
  3. Reusable behavior

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

အဓိက Syntax / Pattern

csharp
class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                name = value;
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "Liam";
        Console.WriteLine(person.Name);
    }
}

Properties ကfield-like syntax နဲ့controlled access ပေးပြီး validation, computed values, `init` နဲ့required initialization ကိုဖော်ပြနိုင်ပါတယ်။ Getter တွင်hidden expensive work/side effects မထားဘဲ nullability နဲ့mutability ကိုAPI contract အဖြစ်သတ်မှတ်ပါ။

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

  1. Object / dependencyclass Person

    Properties များသည် private data ကို လုံခြုံစွာ access လုပ်ရန်အသုံးပြုပါတယ်။ Direct field ကို public ပေးမထားဘဲ get / set မှတစ်ဆင့် ထိန်းချုပ်နိုင်တာကြောင့် validation logic ထည့်ဖို့လည်း အဆင်ပြေပါတယ်။

  2. C# type contract + CLR}

    Properties ကfield-like syntax နဲ့controlled access ပေးပြီး validation, computed values, `init` နဲ့required initialization ကိုဖော်ပြနိုင်ပါတယ်။ Getter တွင်hidden expensive work/side effects မထားဘဲ nullability နဲ့mutability ကိုAPI contract အဖြစ်သတ်မှတ်ပါ။

  3. Reusable behavior}

    Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

Execution Flow
  1. Object / dependency
  2. C# type contract + CLR
  3. Reusable behavior

Success

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

Failure

Auto-property အားလုံးကိုpublic setter ပေးခြင်းကencapsulation ကိုမဖြစ်စေပါ။

Properties များသည် private data ကို လုံခြုံစွာ access လုပ်ရန်အသုံးပြုပါတယ်။ Direct field ကို public ပေးမထားဘဲ get / set မှတစ်ဆင့် ထိန်းချုပ်နိုင်တာကြောင့် validation logic ထည့်ဖို့လည်း အဆင်ပြေပါတယ်။

csharp
class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                name = value;
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "Liam";
        Console.WriteLine(person.Name);
    }
}

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

  • name field ကို private ထားတဲ့အတွက် class အပြင်ကနေတိုက်ရိုက်ပြင်လို့မရပါ။
  • Name property က data ကိုထိန်းချုပ်ပြီး access ပေးပါတယ်။
  • set ထဲမှာ empty string မဖြစ်အောင် စစ်ထားပါတယ်။
You should see
Liam

Real-world usage

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

Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

Mini challenge

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

Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                name = value;
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "Liam";
        Console.WriteLine(person.Name);
    }
}

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

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

Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Object / dependency → C# type contract + CLR → Reusable behavior
  • Properties ကfield-like syntax နဲ့controlled access ပေးပြီး validation, computed values, `init` နဲ့required initialization ကိုဖော်ပြနိုင်ပါတယ်။ Getter တွင်hidden expensive work/side effects မထားဘဲ nullability နဲ့mutability ကိုAPI contract အဖြစ်သတ်မှတ်ပါ။
  • Auto-property အားလုံးကိုpublic setter ပေးခြင်းကencapsulation ကိုမဖြစ်စေပါ။
  • Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

Object-oriented programming in C# — Microsoft Learn

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

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

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

Validated Age property, read-only display property နဲ့init-only identifier ပါသောmodel ရေးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Liam

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

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

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

Properties | Thuta Learning