BCA / B.Tech 9 min read

Delegates & Events in Hindi

Delegates & Events in .NET in Hindi | डेलीगेट्स और इवेंट्स का परिचय :


डेलीगेट्स

  • डेलीगेट्स C# में एक प्रकार के रेफरेंस टाइप होते हैं, जो किसी मेथड को रेफर करते हैं। 
  • दूसरे शब्दों में, डेलीगेट्स का उपयोग किसी मेथड को पॉइंटर की तरह रेफर करने के लिए किया जाता है। डेलीगेट्स का उपयोग तब किया जाता है जब हमें रन-टाइम पर किसी मेथड को कॉल करना हो।
  • डेलीगेट्स को "टाइप सेफ" माना जाता है क्योंकि ये केवल उन्हीं मेथड्स को रेफर कर सकते हैं, जिनकी सिग्नेचर (पैरामीटर और रिटर्न टाइप) डेलीगेट्स की सिग्नेचर से मेल खाती है।
इवेंट्स

  • इवेंट्स C# में डेलीगेट्स पर आधारित एक तंत्र है। इवेंट्स का उपयोग तब किया जाता है, जब एक क्लास को दूसरी क्लास को किसी विशेष स्थिति के बारे में सूचित करना हो।
  • उदाहरण के लिए, जब कोई बटन दबाया जाता है, तो एक "क्लिक" इवेंट ट्रिगर होता है।

डेलीगेट्स और इवेंट्स के साथ काम करने के लिए, हमें निम्न चरणों का पालन करना होता है:

  • डेलीगेट को डिफाइन करें।
  • डेलीगेट का ऑब्जेक्ट बनाएं।
  • इवेंट को डिक्लेयर करें।
  • इवेंट को सब्सक्राइब और ट्रिगर करें।
  • सरल प्रोग्राम: डेलीगेट्स और इवेंट्स का उपयोग

नीचे एक उदाहरण है जिसमें डेलीगेट्स और इवेंट्स को समझाया गया है:

using System;

namespace DelegatesAndEventsExample
{
    // Step 1: Define a delegate
    public delegate void NotifyHandler(string message);

    // Publisher class
    public class Publisher
    {
        // Step 2: Declare an event using the delegate
        public event NotifyHandler Notify;

        public void TriggerEvent(string message)
        {
            // Step 3: Trigger the event
            if (Notify != null)
            {
                Notify(message); // Notify all subscribers
            }
        }
    }

    // Subscriber class
    public class Subscriber
    {
        public void OnNotifyReceived(string message)
        {
            Console.WriteLine("Subscriber received message: " + message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create publisher and subscriber objects
            Publisher publisher = new Publisher();
            Subscriber subscriber = new Subscriber();

            // Step 4: Subscribe to the event
            publisher.Notify += subscriber.OnNotifyReceived;

            // Trigger the event
            publisher.TriggerEvent("Hello, this is a test event!");

            Console.ReadLine();
        }
    }
}

कार्य कैसे होता है:

  • NotifyHandler नामक डेलीगेट को परिभाषित किया गया, जो एक मेथड को पॉइंटर के रूप में रेफर करेगा।
  • Publisher क्लास में एक इवेंट Notify डिक्लेयर किया गया।
  • Subscriber क्लास में OnNotifyReceived मेथड को परिभाषित किया गया, जो इवेंट हैंडलर के रूप में काम करता है।
  • Main() में, Publisher इवेंट को Subscriber के मेथड से सब्सक्राइब किया गया और फिर इवेंट को ट्रिगर किया गया।

आउटपुट:

Subscriber received message: Hello, this is a test event!

यह उदाहरण डेलीगेट्स और इवेंट्स को सरल और व्यावहारिक तरीके से समझाने में मदद करता है।

In this Chapter

Delegates & Events in Hindi
Introduction of .Net in Hindi
Web Services in Hindi | वेब सर्विसेस हिंदी में
WSDL in Hindi | WSDL हिंदी में
Boxing & Unboxing in ADO.NET in Hindi
CLR in Hindi | CLR क्या है?
Common Types System in Hindi
MSIL in Hindi
Assemblies & Class Libraries in Hindi
Project of .Net in Hindi
What is VB.NET and IDE in Hindi | वीबी.नेट क्या है ?
Intermediate Language in Hindi
Object Orientation in Hindi
Managed Execution in Hindi
Rapid Development in Hindi
Windows Presentation Foundation in Hindi
Whats new For .NET framework 3.5?
Windows Workflow Foundation (WWF) in Hindi
Windows Card Space in Hindi
Windows Communication Foundation in Hindi
How To Install and Use The Visual Studio 2008
How to Working With Visual Studio 2008
Types of Visual Studio 2008 in Hindi
Visual Studio 2008 IDE in Hindi
How To Create Console Application in Hindi
Introduction of C# in .NET in Hindi
Classes of .NET With C# in Hindi
Properties of .NET With C# in Hindi
Structs in C# .NET in Hindi
Generic Collections in .NET (C#) in Hindi
Type Safety in Hindi
Nullable Types in .NET in Hindi
ADO.NET in Hindi | ADO.NET क्या है?
SQL Connection Object in Hindi
SQL Command in Hindi
LINQ in Hindi | LINQ क्या है?
What is Using Stored Procedures?
Windows Application in .NET in Hindi | Windows Application क्या है?
BCA || .NET with C# 2023 Paper | MDSU Exam Paper
.NET with C# All Important Questions and Answers in Hindi (MDSU)
BCA || .NET with C# 2025 Paper | MDSU Exam Paper