Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use delegate in C #

Shulou Source: shulou.com Published: 2022-06-02 09:15:59 09月20日 Update

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!

Tags: Events functions quantities variables objects costs users programs articles designs scary important behemoths so that in one way in the middle the number of pieces units places Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Shulou Technology Linux MySQL