Thuta Learning
C#
AdvancedProgrammingအခြေခံ

Interfaces

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

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

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

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

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

Interface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။

Problem → Why → Solution

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

Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။

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

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

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

Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။

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

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

အဓိက Syntax / Pattern

csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။

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

  1. Object / dependencyinterface INotification

    Interface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။

  2. C# type contract + CLRpublic void Send(string message)

    Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။

  3. Reusable behavior}

    IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

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

Success

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

Failure

Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။

Interface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။

csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

Interface အသုံးဝင်တဲ့နေရာ

Notification ကို email, SMS, push notification စတာတွေဖြင့်ပို့နိုင်ပေမယ့် app ရဲ့ main logic က Send() method တစ်ခုကိုပဲသိနေဖို့လိုပါတယ်။ Implementation ပြောင်းချင်ရင် class ပြောင်းလိုက်ရုံပါ။

You should see
Email sent: Welcome to ThutaTech

Real-world usage

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

IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

Mini challenge

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

IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
interface INotification
{
    void Send(string message);
}

class EmailNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"Email sent: {message}");
    }
}

class SmsNotification : INotification
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS sent: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        INotification notifier = new EmailNotification();
        notifier.Send("Welcome to ThutaTech");
    }
}

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

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

IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Object / dependency → C# type contract + CLR → Reusable behavior
  • Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။
  • Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။
  • IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

Object-oriented programming in C# — Microsoft Learn

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

  • Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

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

IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Email sent: Welcome to ThutaTech

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

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

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

Interfaces | Thuta Learning