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 entrust in C #

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

Share

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

This article mainly shows you "how to entrust in C#", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to entrust this article.

First, what is it?

1) the delegate contains a reference to the method instead of the method name. Using a delegate, you can dynamically set the method to be called at run time, do not know the method name, or call the method to execute (or call) the method that the delegate will reference.

2) the delegate connects the name to the definition of the method, attaching the implementation of the method to the name. This allows you to call a specific method with that name. However, the delegate requires that the implementation of the method and the delegate must have the same method signature (that is, they should have the same number / type of parameters) and the same type of return value.

3) the delegate is more like a common method name, pointing the name to different methods in different situations, and executing these methods through the delegate.

Second, how to use it

There are three steps to using a delegate:

1) define delegation

2) instantiate delegation

3) use delegation

Our last example is:

Class Program {/ / defines a delegate public delegate int Call (int num1, int num2); class Math {public int Mutiply (int num1, int num2) {return num1 * num2 } public int Divide (int num1, int num2) {return num1 / num2;}} static void Main (string [] args) {Call objCall; Math objMath = new Math () ObjCall = new Call (objMath.Mutiply); / / instantiate a delegate int result = objCall (5,3); / / use delegate Console.WriteLine ("result is {0}", result);}}

In the above example, let's go a step further and implement it with anonymous delegates:

Static void Main (string [] args) {Call objCall = delegate (int num1, int num2) {return num1 * num2;}; / Anonymous delegate int result = objCall.Invoke (3,5); / / use delegate Console.WriteLine ("result is {0}", result); / / output: result is 15}

We can also:

Int result = objCall.Invoke (3,5); / / use delegation

To be replaced by:

Int result = objCall (3,5); / / use delegation

The effect is the same.

Further, by simplifying with an Lambda expression, you can:

Call objCall = delegate (int num1, int num2) {return num1 * num2;}; / / Anonymous delegation

To be replaced by:

Call objCall = (int num1, int num2) = > {return num1 * num2;}; / / Lambda expression

Simplify it one step further and change it to:

Call objCall = (num1, num2) = > {return num1 * num2;}; / / Lambda expression

This is the evolution of C # delegates and the support of C#Lambda expressions for delegates.

When to use it

1) the delegate is similar to the function pointer in C language, and the method can be passed as the parameter of the function.

2) when you don't know the specific implementation of the method, you can define a delegate to work for us.

3) what we use most when programming is when registering events.

For example:

This.button1.Click + = new EventHandler (button1_Click); / / button1 registers the Click event private void button1_Click (object sender, EventArgs e) {/ / method implementation content}

You can see that the method button1_Click is passed to the EventHandler delegate as an argument.

For example, in the threading method:

Thread th = new Thread (new ThreadStart (Method)); / / the ThreadStart here is a delegate, in which you can directly pass a method name Method and call the method th.IsBackground = true;th.Start () in the form of a delegate. The above is all the content of the article "how to delegate in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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