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:
Do'stlaringiz bilan baham: |