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 implement generic delegation and closure functions in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to implement generic delegation and closure functions in C#. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

We can find an explanation of this so-called generic delegate from MSDN. Delegate is one of the more concepts in C # than Java. Delegate in C # 1.0 is generally a function pointer, but with the development of the language, multicast delegates and events have been derived from delegates.

With reference to the F# function, we actually have a better understanding of generic delegates in C #. For example:

Let x x y = x + y

This F# statement declares a function x that takes two integer parameters and returns an integer value. Its functional characteristics are

Val x: x:int-> y:int-> int

With the help of powerful type inference, we can write functions like mathematical functions. C # is different. C # is not a programming language based on function programming, so we need to mark the types, so it is:

Func x = (j, k) = > j + k

As you can see, the Func we wrote is the eigenvalue of the so-called function in F#. In functional programming, more emphasis is placed on immutability. For example, in F#, the values that we only use let binding are immutable values. If we need to use variables, then we need to complain about variability, that is, we need to use let mutable binding methods to bind values. The use of variable values is not welcome in the field of function programming, because it may bring more hidden dangers, especially global variables, which will make it difficult to control in the later stage of the program.

Although C# now has strict control over scope, there are times when we have to initialize a bunch of variables and refer to them again and again, which can lead to unexpected results if we don't pay attention to them, but with the help of generic delegates, we can incorporate the advantages of functional programming into C#.

For example:

Class Complex {public Double Re {get; set;} = 0; public Double Im {get; set;} = 0; public Double Modulus = > System.Math.Sqrt (Re * Re + Im * Im); private static Func RequestConjugate () = > x = > new Complex {Re = x.Re, Im =-x.Im}; public Complex Conjugate () = > RequestConjugate () (this);}

Notice RequestConjugate, which returns only a generic delegate rather than an actual conjugate complex. While the Conjugate method is very weird RequestConjugate () (this), there are two parentheses, and the second parenthesis is actually passed in x as an argument, so that we can implement a closure, we can dynamically create a function and return it as a return value, and use it where we need it.

The above is how to implement generic delegation and closure functions in C#. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Internet Technology

Wechat

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

12
Report