Thursday, December 20, 2012

Passing Delegate as a method parameter.


Below code demonstrate how to pass a delegate as a function parameter. To understand the delegate basics click here.


public partial class Delegate : System.Web.UI.Page
{
    public delegate string UserStatus(int age);
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterUser("Janaka", Isadult);
    }

    static string Isadult(int age)
    {
        string status=string.Empty;
        status = age>18 ? "Adult":"Young";
        return status;
    }

    private void RegisterUser(string Name, UserStatus stu)
    {
        Response.Write(Name+"<br/>");
        Response.Write(stu(50));
    }
}

Output:
Janaka
Adult


No comments:
Write comments
Recommended Posts × +