• WriteLine
  • ReadKey (); } } } Dastur natijasi: Misol : using
  • Salom (); Console.ReadKey (); } } } Misol : using
  • Show_Message (); message(); Console.ReadKey (); } private
  • Lambda-ifodasining kelib chiqishi




    Download 1 Mb.
    bet7/9
    Sana13.02.2024
    Hajmi1 Mb.
    #156042
    1   2   3   4   5   6   7   8   9
    Bog'liq
    37-mavzu

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    class DemoLambdaTask
    {
    static void Main()
    {
    Task tsk = Task.Factory.StartNew( () => {
    Console.WriteLine("Vazifa boshlandi");
    for (int count = 0; count < 10; count++)
    {
    Thread.Sleep(500);
    Console.WriteLine("Muammolar soni " + count );
    }
    Console.WriteLine("Vazifa tugadi.");
    } );
    tsk.Wait();
    tsk.Dispose();
    Console.WriteLine("Asosiy qism tugadi.");
    Console.ReadKey();
    }
    }
    Dastur natijasi quyidagicha:

    Lambda-ifodalari orqali dasturlar tuzish

    Lambda-ifodalaridan foydalanishda kirish parametrlari turlari aniq yoki barchasi aniq bo'lishi kerak, aks holda CS0748 kompilyator xatosi yuz beradi.



    using System;
    namespace Lambda_1
    {
    class Program
    {
    delegate int Operation(int x, int y);
    static void Main(string[] args)
    {
    Operation operation = (x, y) => x + y;
    Console.WriteLine(operation(10, 20));
    Console.WriteLine(operation(40, 20));
    Console.ReadKey();
    }
    }
    }

    Dastur natijasi:


    1. using System;
      namespace Lambda_2
      {
      class Program
      {
      delegate int kvadrat(int x);
      static void Main(string[] args)
      {
      kvadrat k = i => i * i;
      Console.Write("Sonni kiriting: ");
      int a=int.Parse(Console.ReadLine());
      Console.WriteLine("Uning kvadrati: "+k(a));
      Console.ReadKey();
      }
      }
      }

    Dastur natijasi :

    Misol :
    using System;
    namespace Funk_2
    {
    class Program
    {
    public static int r (int x)
    {
    int y=x*x; return y;
    }
    static void Main(string[] args)
    {
    Console.Write("Sonni kiriting: ");
    int a=int.Parse(Console.ReadLine());
    Console.WriteLine("Uning kvadrati: "+r(a));
    Console.ReadKey();
    }
    }
    }

    Misol :
    using System;
    namespace Lambda_3
    {
    class Program
    {
    delegate void Salom();
    static void Main(string[] args)
    {
    Salom s1 = () => Console.WriteLine("Salom!");
    Salom s2 = () => Console.WriteLine("Xush kelibsiz.");
    s1(); s2();
    Console.ReadKey();
    }
    }
    }

    Dastur natijasi:

    Misol :
    using System;

    namespace Lambda_3
    {
    class Program
    {
    public static void Salom()
    {
    Console.WriteLine("Salom!");
    Console.WriteLine("Xush kelibsiz.");
    }
    static void Main(string[] args)
    {
    Salom();
    Console.ReadKey();
    }
    }
    }

    Misol :
    using System;
    namespace Lambda_4
    {
    class Program
    {
    delegate void ChangeHandler(ref int x);
    static void Main(string[] args)
    {
    Console.Write("Sonni kiriting: ");
    int x = int.Parse(Console.ReadLine());
    ChangeHandler ch = (ref int n) => n = n * 2;
    ch(ref x);
    Console.WriteLine(x);
    Console.ReadKey();
    }
    }
    }
    Dastur natijasi:

    Misol :
    using System;
    namespace Lambda_5
    {
    class Program
    {
    delegate void Hello();
    static void Main(string[] args)
    {
    Hello message = () => Show_Message();
    message();
    Console.ReadKey();
    }
    private static void Show_Message()
    {
    Console.WriteLine("Assalomu alaykum!");
    }
    }
    }

    Misol :
    using System;

    namespace Lambda_6
    {
    class Program
    {
    delegate bool IsEqual(int x);

    static void Main(string[] args)


    {
    int[] integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int result1 = Sum(integers, x => x > 5);
    Console.WriteLine(result1);
    int result2 = Sum(integers, x => x % 2 == 0);
    Console.WriteLine(result2);
    Console.ReadKey();
    }

    private static int Sum (int[] numbers, IsEqual func)
    {
    int result = 0;
    foreach(int i in numbers)
    {
    if (func(i))
    result += i;
    }
    return result;
    }
    }
    }

    Delegatlar metodlarga ishora qiluvchi ob'ektlarni ifodalaydi. Ya'ni, delegatlar - bu metodlarning ko'rsatgichlari va biz ushbu metodlarni delegatlar yordamida chaqirishimiz mumkin bo'ladi.
    Delegatni e'lon qilish uchun delegate kalit so'zi ishlatiladi, undan keyin qaytish turi, nomi va parametrlari yoziladi. Quyida delegat e'lon qilishning umumiy shakli keltirilgan:
    delegate qiymat_turi nomi (parametrlar_ro’yxati); bu yerda qiymat_turi delegat tomonidan chaqiriladigan metodlar bilan qaytarilgan qiymat turini bildiradi; nomi - delegatning aniq nomi; parametrlar_ro’yxati – delegate ob’ekti tomonidan chaqirilgan metodlar uchun zarur bo'lgan parametrlar.Delegat nusxasi ya’ni ob’ekti hosil qilingandan so'ng, qiymat turi va parametrlari delegat e’lon qilinganda ko'rsatilganlariga mos keladigan metodlarni chaqirishi va murojaat qilishi mumkin.
    Eng muhimi, delegatni tegishli signatura va qaytish turiga mos har qanday metodga murojaat qilish uchun ishlatishi mumkin. Bundan tashqari, chaqirilayotgan metod alohida ob'ekt bilan bog'langan sinf metodi yoki ma'lum bir sinf bilan bog'liq statik metod bo'lishi mumkin. Faqat bitta narsa muhimdir: metodning qaytish turi va signaturasi delegat e’lon qilinishida ko'rsatilganlarga mos kelishi kerak.
    Misol keltiramiz:
    using System;
    delegate int Amal (int x, int y);
    class Program
    {
    static int Sum(int x, int y)
    {
    return x + y;
    }
    static int Multiply(int x, int y)
    {
    return x * y;
    }
    static int Sub(int x, int y)
    {
    return x - y;
    }
    static int Div(int x,int y) {
    if (y != 0) return x / y; else return 0;
    }
    static void Main(string[] args)
    {
    int a, b; Amal a1=null;
    Console.Write("Birinchi sonni kiriting: ");
    a = Int32.Parse(Console.ReadLine());
    Console.Write("Ikkinchi sonni kiriting: ");
    b = Int32.Parse(Console.ReadLine());
    Console.Write("Arifmetik amallardin(+, -, *, / ) bittasini kiriting: "); char ch = Char.Parse(Console.ReadLine());
    switch (ch)
    {
    case '+': a1 = Sum; break;
    case '-': a1 = Sub; break;
    case '*': a1 = Multiply; break;
    case '/': a1 = Div; break;
    default: Console.WriteLine("Arifmetik amalni noto'g'ri kiritibsiz. "); break;
    }
    Console.WriteLine("Natija: {0} {1} {2} = {3}", a, ch, b, a1(a,b));
    Console.ReadLine();
    }
    }
    Ushbu misoldan asosiy xulosa shuki, Amal delegat misoli chaqirilgandan so'ng, u murojaat qilgan metod chaqiriladi. Shuning uchun, metodga murojaat kompilyatsiya vaqtida emas, balki ish vaqtida hal qilinadi.
    Delegatlar faqat delegat o'zgaruvchisi aniqlangan sinfda aniqlangan metodlarni ko'rsatishi shart emas, shuningdek, bu boshqa sinflar va strukturalardagi metodlar ham bo'lishi mumkin.
    Using System;
    class Math
    {
    public int Sum(int x, int y) { return x + y; }
    }
    class Program
    {
    delegate int Amal(int x, int y);
    static void Main(string[] args)
    {
    Math math = new Math(); Amal del = math.Sum;
    int result = del(4, 5); // math.Sum(4, 5)
    Console.WriteLine(result); // 9
    Console.Read();
    } }
    Delagatlarning qo’llanilishi using System; class Program
    {
    delegate int Amal(int x, int y);
    static void Main(string[] args)
    {
    Amal del = Add; // 1-chi usul
    Amal del2 = new Amal(Add); // 2-chi usul
    Console.Read();
    }
    private static int Add(int x, int y) { return x + y; } }

    Download 1 Mb.
    1   2   3   4   5   6   7   8   9




    Download 1 Mb.