C#的委托(delegate)和事件(event)

有一些概念是看过一次记得,久不接触就忘了的,但是以后要重新捡起,又要花些时间看书,所以记下来为了巩固。

1. 申明Deleagate:其实背后是IL编译器为你创造了一个封装函数指针的类,这个类名就是你申明时所用的函数名,例如Delegate void AddDelegate(int, int)。winForm技术中喜欢将AddDelegate称为AddEventHandler,所以也就有了那么多EventHandler。

2. 申明Event::当Event和前面申明的委托,也就是AddEventHandler联合申明一个事件,例如 public event AddEventHandler addevent, 这个IL编译出来的的是包含函数指针列表的类实例。而addevent()调用,就变成函数列表的调用,官方文字称诸多订阅该事件的委托实例的调用。所以订阅的语句:addevent+= new AddEventHandler(clientDimAdd) ,也就是将这个回调函数加入到列表中,以便事件触发时依次调用。

26,137人评论了“C#的委托(delegate)和事件(event)”

  1. I like what you guys are up also. Such intelligent work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my web site :).

  2. Hello, Neat post. There’s a problem along with your site in internet explorer, would test this… IE nonetheless is the marketplace chief and a large part of other folks will omit your great writing due to this problem.

  3. Just desire to say your article is as astonishing. The clearness in your
    post is simply spectacular and that i can suppose you’re an expert on this subject.
    Fine along with your permission allow me to take hold of your RSS feed to keep up to date
    with imminent post. Thank you a million and please carry
    on the enjoyable work.

  4. Definitely imagine that which you stated. Your favorite
    reason appeared to be at the net the easiest thing to have in mind
    of. I say to you, I definitely get irked whilst people think about concerns that they plainly do not understand about.

    You managed to hit the nail upon the highest and also defined out the entire thing with no need side effect
    , people can take a signal. Will likely be again to get more.
    Thank you

  5. Greetings! I know this is somewhat off topic but I was wondering which blog
    platform are you using for this website? I’m getting fed up of
    Wordpress because I’ve had problems with hackers and I’m looking at options
    for another platform. I would be fantastic if you could
    point me in the direction of a good platform.

  6. very good post story!! blog!!!Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed browsing your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!

  7. Heya! I just wanted to ask if you ever have any problems with hackers?

    My last blog (wordpress) was hacked and I ended up losing
    several weeks of hard work due to no backup. Do you have any methods to stop hackers?

  8. Hmm is anyone else encountering problems with the images on this
    blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  9. I don’t even know how I ended up here, but I thought this post
    was good. I don’t know who you are but certainly you are going to a famous
    blogger if you are not already 😉 Cheers!

  10. I blog often and I truly thank you for your information. Your article
    has truly peaked my interest. I’m going to take a note of your blog and keep checking for
    new details about once a week. I subscribed to your RSS feed
    as well.

  11. I was recommended this web site by my cousin. I’m not sure whether this post
    is written by him as no one else know such detailed about my trouble.
    You’re incredible! Thanks!

  12. Hello! Would you mind if I share your blog with my facebook
    group? There’s a lot of people that I think would really appreciate
    your content. Please let me know. Thank you

  13. Excellent goods from you, man. I have understand your stuff previous to and you’re just extremely excellent.
    I really like what you’ve acquired here, certainly like what you’re stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it wise.

    I can’t wait to read much more from you.

    This is really a wonderful site.

  14. Hello! I’ve been following your site for some time now and finally got
    the courage to go ahead and give you a shout out from Huffman Tx!
    Just wanted to say keep up the excellent work!

  15. I like what you guys are up too. Such intelligent work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my site :).

  16. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your weblog?
    My blog site is in the very same niche as yours and my users would really benefit from some of the information you present here.

    Please let me know if this okay with you. Thank you!

  17. Whats up very nice website!! Guy .. Excellent .. Superb .. I’ll bookmark your website and take the feeds additionally…I’m glad to search out so many helpful information here within the post, we’d like develop extra techniques on this regard, thanks for sharing.

发表评论

邮箱地址不会被公开。 必填项已用*标注