Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use delegate in C #

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use delegate in C#. I hope you will get something after reading this article. Let's discuss it together.

The C # language has a lot to learn. Here we mainly introduce C # delegate, including the variable cost of the number of Windows Message.

Event handling is a very important part of component design. In C #, events are closely related to C # delegate. First, look at a simple event example.

Using System; namespace EventDemo {public delegate void ProcessHandler (object sender); public class Class1 {private event ProcessHandler _ processHandler = null; public event ProcessHandler ProcessStart {add {_ processHandler + = value;} remove {_ processHandler-= value;}} public void Process () {_ processHandler (this); for (int I = 0; I < 10; iTunes +) ii = iTun1;} public Class1 () {}

C # delegate acts as a function pointer. A user can add a function to a delegate, while a delegate allows a user to join more than one function. When calling this delegate, it is equivalent to calling all the functions contained in it. However, there is a hidden problem with the design approach, that is, when there are a large number of events, the object must pay the corresponding number of C # delegate variables.

Private event ProcessHandler _ processStart = null; private event ProcessHandler _ processEnd = null; private event ProcessHandler _ processStep = null

Regardless of whether the user uses these events or not, they have to pay these costs when the object is created, which is even more frightening in window applications, because the number of Windows Message (window messages) is in thousands. If a simple window program has to pay a variable cost relative to the number of Windows Message, then the object will become a behemoth.

After reading this article, I believe you have a certain understanding of "how to use delegate in C#". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report