Event & EXTENSION METHODS


protected void Page_Load(object sender, EventArgs e) { Button objC = new Button(); } protected void Number_Handler(object sender, CommandEventArgs e) { switch (e.CommandName) { case "NAT": LoadNumbers(0, 1); break; case "EVE": LoadNumbers(0, 2); break; case "ODD": LoadNumbers(1, 2); break; } } private void LoadNumbers(int stratNumber, int stepNumber) { ListBox1.Items.Clear(); for (int i = stratNumber; i <= 100; i = i + stepNumber) { ListBox1.Items.Add(i.ToString()); } } ================================================ EVENTS ====== what is an event? An action recognized by an object. Event_handler contains the logic to handle the action. protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new Employee.SalaryDelegate(myhandler); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void myhandler(string EmpName, int Salary) { Response.Write(EmpName + " Salary " + Salary.ToString() + " is invalid"); } ========== public class Employee { public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event SalaryDelegate SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(EmpName, value); } } } } ================= namespace WebApplication17 { public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(this, new EventArgs()); //SalaryEvent(null, null); } } } } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EventArgs e) { Employee tmpEmp = sender as Employee; Response.Write(tmpEmp.EmpName + " salary is invalid" ); } =================== public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(null, new EmpEventData(this.EmpName, value)); } } } } public class EmpEventData : EventArgs { public string Name; public int salary; public EmpEventData(string pName, int psalary) { this.Name = pName; this.salary = psalary; } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EmpEventData e) { Response.Write(e.Name + " salar " + e.salary.ToString() + " is invalid"); } } =================== EXTENSION METHODS ================= 1) If no source code available 2) the class is sealed class but still you want to extend the functionalty of the clas then you can go for extentsion methods namespace StringExtender { public static class StringExtender { public static string ToProper(this string strValue) { StringBuilder sb = new StringBuilder(); string[] myWords = strValue.Split(' '); foreach (string st in myWords) { sb.Append(st.Substring(0, 1).ToUpper()); sb.Append(st.Substring(1).ToLower()); sb.Append(" "); } return sb.ToString(); } } } using StringExtender; namespace WebApplication17 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string st = "ravi kuMar KVS, gss Info TEch, Hyderabad"; Response.Write(st.ToProper()); } } }

Delegate


It is a class. Un like a class it contains signature.
And it is capable to hold the methods which are matches to its signature.
When you invoke the deleage it executes method what it holds.


1) You can send methods as parameters to another method

 Different Types of delegates

 Annoymous methods : allows you to create methods on the fly without name and assign them to delegate.

 Lamda Expressions and statements 


 protected void Page_Load(object sender, EventArgs e)
        {
            MyDelegate objDel = BgNuimber;
            Response.Write(objDel(34, 6));
            Response.Write("
");

            //anonymous method
           //objDel = delegate(int x, int y) { return x + y; };

            //lamda Expression
            //objDel = (int x, int y) => x + y;
            objDel = (x, y) => 6;
            
            Response.Write(objDel(6, 6));

            //RunMethods(BgNuimber, 565, 34);
            //RunMethods(ProductNumbers, 4, 4);
        
        }

        public void RunMethods(MyDelegate myDel, int x, int y)
        {
            Response.Write(myDel(x,y));
            Response.Write("
");
        }


        public int BgNuimber(int a, int b)
        {
            int bigNumber;

            if (a > b)
                bigNumber = a;
            else
                bigNumber = b;

            return bigNumber;
        }

        public int ProductNumbers(int x, int y)
        {
            return x * y;
        }


     if you want a return value method binding to delegate --> goto function delegates
            Func myFucionDel = BgNuimber;
            Response.Write(myFucionDel(34, 356));


     if you want void method binding to delegate --> goto function delegates
            Action myActionDel = delegate(int no, string name) { Response.Write("Your Number is : " + no.ToString() + "
" + "Your name is " + name); };
                myActionDel(5486," Ravi KVS");


        protected void Page_Load(object sender, EventArgs e)
        {
            List myList = new List() { 344, 6, 222, 9, 11,     };

            var newList = myList.Where((n) =>
                {
                    int r;
                    int rev = 0;
                    int m = n;

                    while (m != 0)
                    {
                        r = m % 10;
                        rev = rev * 10 + r;
                        m = m / 10;
                    }
                    return n == rev;
                });

            //Code to check even numbers
            //var newList =  myList.Where((n) => n % 2 == 0);
            //var newList = myList.Where((n) => CheckEven(n));

            //Code to return alternate Numbers
            //var newList =  myList.Where((n, i) => i % 2 != 0); 
            foreach (var n in newList)
            {
                Response.Write(n);
                Response.Write("
");
            }
        }