استفاده از delegate برای فراخوانی object method ها در #C
سلام بر تمامی برنامه نویسان سی شارپ 😉 در این مطلب ، استفاده از delegate برای فراخوانی object method ها در سی شارپ #C رو براتون آماده کردم. انشاالله که این کد براتون مفید باشه.
خروجی پروژه به این صورت می باشد:
personDescription = Price is 32 years old
M is 140 mph
M is 140 mph
استفاده از delegate برای فراخوانی object method ها :
using System;
public delegate string DelegateDescription();
public class Person
{
private string name;
private int age;
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public string NameAndAge()
{
return(name + " is " + age + " years old");
}
}
public class Employee
{
private string name;
private int number;
public Employee(string name, int number)
{
this.name = name;
this.number = number;
}
public string MakeAndNumber()
{
return(name + " is " + number + " mph");
}
}
class MainClass
{
public static void Main()
{
Person myPerson = new Person("Price", 32);
DelegateDescription myDelegateDescription = new DelegateDescription(myPerson.NameAndAge);
public delegate string DelegateDescription();
public class Person
{
private string name;
private int age;
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public string NameAndAge()
{
return(name + " is " + age + " years old");
}
}
public class Employee
{
private string name;
private int number;
public Employee(string name, int number)
{
this.name = name;
this.number = number;
}
public string MakeAndNumber()
{
return(name + " is " + number + " mph");
}
}
class MainClass
{
public static void Main()
{
Person myPerson = new Person("Price", 32);
DelegateDescription myDelegateDescription = new DelegateDescription(myPerson.NameAndAge);