Anonymous Methods in C#

Anonymous Methods in C#: Anonymous Method एक ऐसा Method होता है, जिसका कोई नाम नहीं होता। दूसरे शब्दों में कहें तो एक Unnamed Statement Block को Anonymous Method कहा जा सकता है।

कभी-कभी हमें ऐसे Code Blocks की जरूरत होती है, जिन्हें पूरे Program के दौरान केवल एक ही बार Execute किया जाना होता है। इस स्थिति में हमें Named Method Define करने की जरूरत नहीं होती, बल्कि हम ऐसा Method Define कर सकते हैं, जिसका कोई नाम न हो और उस Method का Pointer सीधे ही किसी Delegate Object को Assign या Initialize कर सकते हैं। इस प्रकार के Anonymous Methods, One-Time Method भी कहलाते हैं।

Modern Programming में Anonymous Methods का बहुत प्रयोग किया जाता है, क्योंकि ये One-Time Method होते हैं, जो एक बार Execute होने के लिए Create किए जाते हैं और Execution के बाद Memory से पूरी तरह से Release हो जाते हैं।

Anonymous Method के Concept को हम निम्न Program द्वारा आसानी से समझ सकते हैं, जहां हम MultipleCallbackMethodInvocation.cs Program को ही फिर से Modify कर रहे हैं:

File Name: AnonymousMethod.cs
using System;

namespace CSharpDelegate
{
    delegate void MyDelegate();	//Delegate Declaration

    class AnonymousMethod
    {
        static void Main()
        {
            MyDelegate callMethod = new MyDelegate(delegate()
            {
                Console.WriteLine("This is Display() Method.");
            });

            callMethod += delegate()
            {
                Console.WriteLine("This is Hello() Method.");
            };

            callMethod += delegate()
            {
                Console.WriteLine("This is Hi() Method.");
            };

            callMethod += delegate()
            {
                Console.WriteLine("This is Bye Method.");
            };

            callMethod();
        }
    }
}

Output:
	This is Display() Method.
	This is Hello() Method.
	This is Hi() Method.
	This is Bye Method.

जैसाकि इस Program के Output द्वारा हम समझ सकते हैं कि हमें Exactly वही Output प्राप्त हो रहा है, जो MultipleCallbackMethodInvocation.cs Program से प्राप्त हो रहा था, लेकिन इस Program में हमने एक भी Named Method Create नहीं किया है, बल्कि सभी Methods, Unnamed Anonymous Methods हैं।

Anonymous Method को हमेंशा सीधे ही किसी Delegate को Initialize या Assign करना होता है तथा Anonymous Method Create करने के लिए हमें Method के नाम के स्थान पर delegate Keyword को Specify करना होता है। जबकि यदि Anonymous Method कोई Parameter Accept करता है तो वे सभी Parameters हमें delegate Keyword के बाद Parenthesis के बीच Specify करने होते हैं। जैसे:

delegate(12, 21)

इसी तरह से यदि Anonymous Method कोई Value Return करता है, तो उस Value को हम उसी तरह से उपयोग में ले सकते हैं, जिस तरह से Normal या Static Method द्वारा Returned Value को किसी Variable में Store करके अथवा Directly Use करते हैं। इस प्रकार से एक Anonymous Method का Syntax कुछ निम्नानुसार होता है:

DelegateObjectReferenceVariable = delegate()
{
       Console.WriteLine("This is Hello() Method.");
};

चूंकि एक Anonymous Method के साथ किसी तरह का कोई Return Type Specify नहीं किया जाताए इसलिए यदि Anonymous Method कोई Value Return करता है, तो Return होने वाले Value का Type ही Anonymous Method का Return Type होता है। जैसे:

DelegateObjectReferenceVariable = delegate()
{
       return 10;
};

ये Anonymous Method मान 10 Return कर रहा है जो कि एक Integer Type का मान है, इसलिए इस Anonymous Method का Return Type int है।

यदि हम चाहें तो ऐसा Delegate Create कर सकते हैं, जो अपने अन्तिम Parameter के रूप में params Type Parameter Accept करता हो। इस स्थिति में हम जिस किसी भी Callback Method को इस Delegate Object में Assign करते हैं, उन सभी में params Parameter का Specified होना जरूरी होता है। लेकिन Anonymous Methods के Parenthesis Memory में params Keyword Specify नहीं करना चाहिए। जैसे:

Anonymous Methods in C# - Hindi

Variable Scope for Anonymous Methods in C#

जब हम किसी Anonymous Method में कोई Variable Declare करते हैं, तब हमें इस बात का ध्‍यान रखना चाहिए कि Anonymous Method एक One-Time Method होता है, जो एक बार Execute होने के बाद Destroy हो जाता है।

इसलिए यदि हम किसी Variable को किसी Anonymous Method में Declare करते हैं, तो उस Variable को हम Anonymous Method के अलावा और कहीं पर भी Use नहीं कर सकते। लेकिन Current Scope में जितने भी Outer Variables होते हैं, जिन्हें Anonymous Method के बाहर Declare किया गया होता है, उन्हें Anonymous Method Access कर सकता है।

Anonymous Method जब किसी Outer Variable को Use करता है, तो उस Variable को Captured Variable कहा जाता है। Captured Variable तब तक Anonymous Method के लिए Available रहता है, जब तक कि Anonymous Method पूरी तरह से Destroy नहीं हो जाता।

अत: हालांकि C# में Block से Exit होते ही उस Block का Scope समाप्त हो जाता है और उस Block के सभी Variables व Objects पूरी तरह से Destroy हो जाते हैं। लेकिन यदि किसी Local Variable को किसी Anonymous Method द्वारा Use किया जा रहा हो, तो Block से Exit होने के बाद भी वह Variable उस Anonymous Method के लिए तब तक Exist रहता है, जब तक कि उस Anonymous Method को Delegate द्वारा Refer किया जा रहा होता है।

C# Delegate Example
What is Delegate in C#

C# in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

C#.NET in Hindi | Page:908 | Format: PDF

BUY NOW GET DEMO REVIEWS

MidRangeHub Finance Auto Loans Bank Business Health Insurance Insurance Investment Law Lif Insurance Loan Stocks how to endorse a check chase sapphire travel insurance chase sapphire travel delay when are property taxes due Tower Loans how to sell stocks on cash app Voided Check Examples Personal Finance Books Collateral Loans how to sell stocks on cashapp how do you sell your stocks on cash app how to sell stock on cash app joint account sofi joint account ally joint account capital one joint account best bank for joint account chase joint account cyber insurance coverage silverfort free cyber insurance coverage silverfort monjouro savings card Money6x Real Estate