|
Mirzo Ulug‘bek nomidagi O‘zbekiston Milliy universiteti Amaliy matematika va intellektual texnologiyalar
|
bet | 2/7 | Sana | 19.01.2024 | Hajmi | 293,8 Kb. | | #141255 |
Bog'liq Ibrohimova Sumayya Hamza qiziMisol:
// C# program to illustrate how to
// create an anonymous function
using System;
class GFG {
public delegate void petanim(string pet);
// Main method
static public void Main()
{
// An anonymous method with one parameter
petanim p = delegate(string mypet)
{
Console.WriteLine("My favorite pet is: {0}",
mypet);
};
p("Dog");
}
}
|
Chiqish:
Mening sevimli uy hayvonim: It
Muhim nuqtalar:
Ushbu usul, shuningdek, inline delegate sifatida ham tanilgan .
Ushbu usul yordamida siz alohida usullarni yozmasdan delegat ob'ektini yaratishingiz mumkin.
Bu usul tashqi usulda mavjud o'zgaruvchiga kirishi mumkin. Bunday turdagi o'zgaruvchilar tashqi o'zgaruvchilar sifatida tanilgan . Quyidagi misolda ko'rsatilganidek, fav tashqi o'zgaruvchidir.
Misol:
// C# program to illustrate how an
// anonymous function access variable
// defined in outer method
using System;
class GFG {
// Create a delegate
public delegate void petanim(string pet);
// Main method
static public void Main()
{
string fav = "Rabbit";
// Anonymous method with one parameter
petanim p = delegate(string mypet)
{
Console.WriteLine("My favorite pet is {0}.",
mypet);
// Accessing variable defined
// outside the anonymous function
Console.WriteLine("And I like {0} also.", fav);
};
p("Dog");
}
}
|
Chiqish:
Mening sevimli uy hayvonim - It.
Va menga quyon ham yoqadi.
Bu usulni delegatni parametr sifatida qabul qiladigan boshqa usulga o'tkazishingiz mumkin. Quyidagi misolda ko'rsatilganidek:
|
| |