Skip to main content

Chapter 2.2: Object-Oriented Programming with C#: Understanding Interfaces, Delegates, and Events in C#

 Introduction:

In our previous article, we explored classes, objects, and inheritance in the context of Object-Oriented Programming with C#. In this article, we'll delve into interfaces, delegates, and events, which are crucial concepts for designing flexible, modular, and maintainable applications in C#. In the next article, we'll cover constructors and destructors.


Interfaces:

An interface is a contract that defines a set of methods and properties a class must implement. Interfaces promote loose coupling and ensure that classes adhere to a specific structure. To define an interface, use the interface keyword followed by the interface name:

interface IPrintable
{
    void Print();
}

To implement an interface in a class, use the : symbol followed by the interface name:

class Document : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Printing the document...");
    }
}


Delegates:

A delegate is a type-safe function pointer that can reference a method with a specific signature. Delegates allow you to pass methods as arguments to other methods, enabling a higher degree of flexibility and modularity in your code. To declare a delegate, use the delegate keyword followed by the method signature:

delegate void PrintDelegate(string message);

Example of using a delegate:

static void Main()
{
    PrintDelegate print = PrintToConsole;
    print("Hello, world!");
}

static void PrintToConsole(string message)
{
    Console.WriteLine(message);
}


Events:

Events allow a class to notify other classes when something happens, adhering to the publisher-subscriber pattern. Events are typically used in conjunction with delegates. To declare an event, use the event keyword followed by the delegate type and the event name:

class Publisher
{
    public delegate void EventHandler(string message);
    public event EventHandler OnPublish;

    public void Publish(string message)
    {
        OnPublish?.Invoke(message);
    }
}

Subscribing to and handling the event:

static void Main()
{
    Publisher publisher = new Publisher();
    publisher.OnPublish += HandlePublishEvent;
    publisher.Publish("Event published!");
}

static void HandlePublishEvent(string message)
{
    Console.WriteLine("Received event: " + message);
}


Conclusion:

In this article, we covered the concepts of interfaces, delegates, and events in C#. Understanding these concepts is essential for designing flexible and modular applications in C#. In the next article, we'll discuss constructors and destructors, which are crucial components of object lifecycle management in C#. Stay tuned for more C# learning!

Comments

Some of My Bests

ডাটা স্ট্রাকচার- স্ট্যাক (Stack)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা স্ট্যাক বুঝানোর জন্য। স্ট্যাক খুবই ইন্টেরেস্টিং একটা ডাটা স্ট্রাকচার। অনেক কারনেই এটা ব্যাবহার হয়। আগে স্ট্যাক কনসেপ্টটা নিয়ে আলোচনা করা যাক। স্ট্যাক বলতে বোঝায় একটার উপর একটা সাজায়ে রাখা। বিয়ের বাড়িতে আগে মেলামাইনের গ্লাস দেখা যেত একটার ভেতর আরেকটা ঢুকায়ে লম্বা একটা পাইল তৈরি করে একসাথে ক্যারি করা হচ্ছে। এটা একটা স্ট্যাক। প্রোগ্রামিং এর ভাষায় স্ট্যাক এক্সাক্টলি সেইম জিনিসই। তবে একটু ঘষামাজা আছে। আরেকটা উদাহরণ দেয়া যেতে পারে। বয়ামের ভেতর একটার পর আরেকটা বিস্কিট ঢুকিয়ে রাখা হয়। হ্যা, এইটা পারফেক্ট উদাহরণ হয়েছে। এই

ডাটা স্ট্রাকচার- কিউ (Queue)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা কিউ বুঝানোর জন্য। কিউ জিনিসটার সাথে আমরা সবাই পরিচিত। জীবনে আমরা সবাই কখনো না কখনো লাইনে দাঁড়ায়ছি। কিউ এর বেসিক ক্যারেক্টারিস্টিকসের সাথে মিলিয়েই প্রোগ্রামিং এ কিউ এর কনসেপ্ট। বাস্তব জীবনে একটা কিউ তে কি হয়? সবাই লাইন ধরে দাঁড়ায় কিছু একটা কারনে। যে সবার আগে দাঁড়ায় সেই সবার আগে কার্জসিদ্ধি করে। সবার পরের জন সবার পরে। স্ট্যাকের ক্ষেত্রে আমরা পড়েছিলাম Last In First Out (LIFO) or First In Last Out (FILO)। কেমন আনফেয়ার শোনায় না? সবার পরে আসবে, আবার সবার আগে চলে যাবে। অ্যাটলিস্ট আমার আনফেয়ার লেগেছিলো যখন স্ট্যাক শিখছিল

কম্পিউটার বচন

কম্পিউটারের সাথে আমার পরিচয় খুব ছোট বয়সে না। অন্তত চেনাপরিচিত বন্ধুমহলের অনেকের তুলনায় আমি বাচ্চাই বলা চলে। ক্লাস ২ বা ৩ তে পড়ি যখন ফুফাতো ভাই প্রথম কম্পিউটার কেনে। ফুফুর বাসা কাছেই হওয়ায় মাঝে মাঝেই সেটা দেখার সৌভাগ্য হতো। কিন্তু ছুয়ে দেখার সাহস তখনও হয়নি। সেই বছর থেকে আমাদের রেওয়াজ, ঈদের দিন নামাজ পড়েই সোজা ভাইয়ার রুমে। ভাইয়া নতুন একটা গেম ইন্সটল করে রাখতো আর আমরা কাজিনেরা লাইন দিয়ে সেটা খেলতাম (যদিও আমার ভাগে কমই পড়তো :p )  ক্লাস সিক্সে নতুন স্কুলে ভর্তি হলাম। এ এক আজব জায়গা। সবাই বড়লোকের পোলাপাইন। কতজন গাড়িতে করে আসে। তাদের তো কথাই নাই। যাদের গাড়ি নাই তাদেরও রোজকার বাজেট বিশাল। ইচ্ছামত উড়ায়। আর আমি মধ্যবিত্তের ছেলে। যাওয়া আসার ভাড়া বাদে কোনো টাকা পেতামনা। টিফিন বাসা থেকে বানায়ে দিতো। এই যখন আমার অবস্থা তখন ক্লাসমেটরা নিত্যনতুন গ্যাজেটের সাথে শুধু পরিচয়ই না, পকেটে নিয়ে ঘুরে (যদিও স্কুলে নিষিদ্ধ ছিলো )। সবাই এসব নিয়ে কত গল্প চালায় যায়। আর আমার মত বোকা কিছু পাবলিক হা করে এদের গল্প শুনি। কম্পিউটারের কত বচন, কত আড্ডা। এসব আড্ডায় ঢুকতে কত ইচ্ছা করতো, কিন্তু ঢুকবো কি করে? আমার কম্পিউটা