برمجة متقدمة - سي شارب الدالة المجهولة أو الغير معروفة C# - Anonymous Methods

الدالة المجهولة أو الغير معروفة Anonymous Methods

برمجة متقدمة - سي شارب الدالة المجهولة أو الغير معروفة C# - Anonymous Methods

برمجة متقدمة - سي شارب الدالة المجهولة أو الغير معروفة C# - Anonymous Methods


الدالة المجهولة أو الغير معروفة Anonymous Methods

لقد نقشنا سابقا التفويض الذي يستخدم لتنفيذ الدالة عن طريق مرجعها بدلا من تنفيذها مباشرة.
الدالة المجهولة أو الغير معروفة تقنية تستخدم لتمرير كتلة برمجية كابراميتر تفويض .الدالة المجهولة أو الغيرمعروفة هي دالة لا تحمل اسم.ولكن فقط جسم الدالة.لا يوجد حاجة لتحديد نوع البيانات التي تعيده الدالة ولكن يتم تحديده في الكود البرمجي.

صيغة الدالة الغير معروفة او المجهولة

delegate void NumberChanger(int n);
...
NumberChanger nc = delegate(int x)
{
    Console.WriteLine("Anonymous Method: {0}", x);
};

مثال الدالة الغير معروفة أو المجهولة

using System;

delegate void NumberChanger(int n);
namespace DelegateAppl
{
    class TestDelegate
    {
        static int num = 10;
        public static void AddNum(int p)
        {
            num += p;
            Console.WriteLine("Named Method: {0}", num);
        }

        public static void MultNum(int q)
        {
            num *= q;
            Console.WriteLine("Named Method: {0}", num);
        }
        public static int getNum()
        {
            return num;
        }

        static void Main(string[] args)
        {
            //create delegate instances using anonymous method
            NumberChanger nc = delegate(int x)
            {
               Console.WriteLine("Anonymous Method: {0}", x);
            };
            
            //calling the delegate using the anonymous method 
            nc(10);

            //instantiating the delegate using the named methods 
            nc =  new NumberChanger(AddNum);
            
            //calling the delegate using the named methods 
            nc(5);

            //instantiating the delegate using another named methods 
            nc =  new NumberChanger(MultNum);
            
            //calling the delegate using the named methods 
            nc(2);
            Console.ReadKey();
        }
    }
}

الناتج الدالة الغير معروفة أو المجهولة 

Anonymous Method: 10
Named Method: 15
Named Method: 30
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Ahmed Ata Almahallawi
Freelancer
IT Help Desk,
SEO experience,PHP,C#,ASPX,Business Analysis.
Al alami st
gaza -jabaliaGaza Strip
Palestine
ahmed.almahallawi@gmail.com
DOB: 05/10/1984
by +Ahmed Almahallawi 
29/3/2014