Use a delegate to call object methods : Delegate : Delegate C# Examples


C# Examples » Delegate » Delegate »

 

Use a delegate to call object methods









    
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);

        string  personDescription  =  myDelegateDescription();
        Console.WriteLine("personDescription  =  "  +  personDescription);

        Employee  myEmployee  =  new  Employee("M",  140);

        myDelegateDescription  =  new  DelegateDescription(myEmployee.MakeAndNumber);

        string  d  =  myDelegateDescription();
        Console.WriteLine(d);
    }
}
    
   
  
   



Output

personDescription = Price is 32 years old
M is 140 mph


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate