delegate in C# hindi

C# delegates in Hindi 

इस post मे हम C# delegates को study करेंगे !

जैसा कि हम जानते हैं कि एक function मे एक या एक से अधिक parameter हो सकते हैं, एक function मे हम विभिन्न parameter pass कर सकते हैं!

क्या कभी आपने सुना है कि आप एक function को parameter की तरह pass कर सकते हो! जी हां आप C # मे एक function को Delegate की help से  as a parameter pass कर सकते हो!

आइये जानते है कि delegate क्या होता है ;

C# delegates जो है वह C, C++ में उपस्थित function pointer की तरह होता है, जैसा कि pointers आपने C, C++ मे पढ़ा होगा ! सामन्यतया pointers का काम reference को hold करना होता है!

C# delegate एक reference type वेरिएबल की तरह होता है जो की एक method के reference को hold करता है! और ये reference रनटाइम में change हो सकता है. सारे delegates System.Delegate Class से derive होते हैं !

अगर आप अपने program मे delegate use करना चाहते हैं तो आप को delegate keyword use करना होगा !

How to declare c# Delegates :

<access modifier> delegate <return type> <delegate_name>(<parameters>)

ऊपर दिये गये Syntax मे पहले आप को access modifier प्रयोग करना होगा फिर delegate keyword , फिर return type , फिर delegate name और फिर parameters!

Delegates को type safe function pointer भी कहते हैं क्यूंकि जो return type फंक्शन का होता है वही return type delegate का भी होता है!

आइये Delegate का deceleration एक example से सिखते हैं

Example: Declare delegate

public delegate void Add(int a , int b);

ऊपर दी गयी line मे Add नाम का एक delegate है इस delegate को आप किसी भी method का reference hold करने के लिये use कर सकते हैं  जिसका return type और parameters delegate के समान हो!

आइये इसको एक example की help से समझते हैं;

declare c# delegates in hindi

Output:

c# delegates example output in hindi

ऊपर दिये गये example मे Add नाम का एक delegate बनाया गया है जिसका return type void और ये दो parameters accept कर रहा है! यह उस method का reference accept करेगा जिसका return type void और जो Two parameters accept करता हो !

जैसा कि आप example मे देख रहे हो उसमे Two methods हैं जिसका नाम AddNumber और MultiplyNumber है Add नाम का delegate इन दोनों methods का reference hold कर सकता है!

ऊपर दिये गये example मे हमने Add  Del type का एक reference variable बनाया है जो पहले AddNumber method का reference hold करके call करता है, फिर MultiplyNumber का reference hold करके उसको call करता है!

नीचे दी गयी image से आप delegate को समझ सकते हो

c# Delegates

HOW TO CALL c# Delegates :

आप delegate को एक method की तरह call कर सकते हो क्यूंकि यह method का reference hold करता है!

Example :

HOW TO CALL c# Delegate in hindi

हम delegate को as a parameter भी pass कर सकते हैं !

एक method के पास delegate type का parameter भी हो सकता है !

आइये इसको एक example से समझ सकते हैं ;

Example: Delegate as a Parameter

public static void Adding (Add delegateFunction , int number1 , int number2)

{

delegateFunction (number1 , number2);

}

ऊपर दिए गये example मे Adding method के पास Add type का एक parameter है जोकी delegate को as a function call करता है कुछ इस तरह से delegateFunction (number1 , number2)

आइये इसको एक example से समझते हैं ;

c# Delegates as a Parameter

output

c# Delegates as a Parameter output

Multicast Delegates 

आइये अब जानते हैं कि Multicast delegate क्या होता है ;

एक delegate एक से ज्यादा methods को point कर सकता है इसका मतलब यह है कि एक delegate एक से ज्यादा methods का address अपने पास रख सकता है और उनको one by one call कर सकता है ! अर्थात् एक delegate जो multiple methods को point करता है उसको multicast delegate कहते है !

इसके लिए सबसे पहले हम delegate object create करते हैं फिर इस object मे “+” sign की help से methods को add करते हैं ! और अगर आप method को object से remove करना चाहते हो तो आप को “-” sign को use करना पड़ेगा !

आइये इसको एक example की help से समझते हैं ;

Multicast Delegate Example

Multicast Delegate Example

output:-

Multicast Delegate Example output

ऊपर दिये गये example मे सबसे पहले हमने Add type delegate का object create किया जिसका नाम है addDel ! अब इस addDel object मे हम AddNumber method का reference add करते हैं इसके बाद MultiplyNumber method का reference add करते हैं और फिर DivideNumber का reference add करते हैं !

अब इस addDel object मे तीन methods का reference है जिनको ये object one by one call करता है ! जैसा कि example मे दिखाया गया है ! इस तरह से हम Multicast Delegate को Implement करते हैं !

निवेदन:- आपको c# delegates का यह पोस्ट अच्छा लगा हो तो इसे अपने दोस्तों के साथ share करें. तथा अगर आपका c# delegates को लेकर कोई सवाल हो तो आप कमेंट करके पूछ सकते है. धन्यवाद.

Leave a Comment