Interfeys toifalari tushunchasi Interfeys toifalarni abstract bazaviy sinflar bilan taqqoslash Maxsus interfeyslarni kiritish


public abstract class MulticastDelegate : Delegate {


Download 0.77 Mb.
bet33/50
Sana26.01.2023
Hajmi0.77 Mb.
#1129439
1   ...   29   30   31   32   33   34   35   36   ...   50
Bog'liq
MAVZULAR UMUMIY

public abstract class MulticastDelegate : Delegate
{
// Delegat “ishora qilayotgan” metodlar ro’yxatini qaytaradi.
public sealed override Delegate[] GetlnvocationList ();
// Qoshimcha yuklangan amallar
public static bool operator ==(MulticastDelegate dl, MulticastDelegate d2) ;
public static bool operator !=(MulticastDelegate dl, MulticastDelegate d2);

// Delegat qo’llab-quvvatlaydigan metodlar ro’yxatini boshqarish uchun
// ichki tartibda ishlatiladi
private IntPtr _invocationCount;
private object _invocationList;
}


Oddiy delegatga misol
Quyida delegatga misol keltirilgan:

namespace SimpleDelegate
{
// Bu delegat 2 ta butun qiymat qabul qiluvchi va butun qiymat
// qaytaruvchi istalgan metodga ishora qilishi mumkin
public delegate int BinaryOp(int x, mt y) ;
// Bu sinf BinaryOp ishora qiladigan
// metodlarni o’z ichiga oladi
public class SimpleMath
{
public static int Add (int x, int y) => x + y;
public static int Subtract (int x, int y) => x - y;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine (”***** Oddiy delegatga misol *****\n");

// SimpleMath.Add() ga “ishora qiluvchi”
// delegat yaratish
BinaryOp b = new BinaryOp(SimpleMath.Add);
// Delegatdan foydalanib Add() metodini chaqirish
Console.WriteLine(”10 + 10 is {0}", b(10, 10));
Console.ReadLine ();
}
}
}

Oxirgi qatordan bitta oldingi qatorni quyidagicha yozish ham mumkin:
Console .WnteLine (" 10 + 10 is {0}", b.Invoke(10, 10));
Delegatlar toifalarga nisbatan havfsiz islashini aytgandik. Shunga misol:
public class SimpleMath
{
public static int SquareNumber (int a) => a * a;
}

U holda quyidagi kod xatolik keltirib chiqaradi:

Download 0.77 Mb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   ...   50




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling