简单的事件、委托处理机制

public partial class Member_Text : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
      {
            Books books = new Books();
            //创建委托|撤销事件用-=
            books.nameChange += new Books.nameChangeHender(books_nameChange);
            books.Name = "swollaw";
            Response.Write(books.Name);
      }
 
      void books_nameChange()
      {
            Response.Write("事件正常触发<br>");
      }
    
      public class Books
      {
            private string name;
            public Books()
            {
            }
            public Books(string name)
            {     
                  this.name = name;
            }
 
            //声明委托,可以在类的外部声明
            public delegate void nameChangeHender();
            //声明事件
            public event nameChangeHender nameChange;
 
            public string Name
            {
                  get 
                  { 
                        return name; 
                  }
                  set 
                  { 
                        name = value;
                        //触发事件
                        nameChange(); 
                   }
            }

      }
}

你可能感兴趣的:(事件)