In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize the Func delegation of C#". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is Func entrustment
A Func delegate represents a delegate with a return type
II. Definition of Func delegation
View the definition of Func:
Using System.Runtime.CompilerServices;namespace System {/ Summary: / / encapsulates a method that takes two parameters and returns a value of the type specified by the TResult parameter. / / parameter: / / arg1: / / the first parameter of the method encapsulated by this delegate. / arg2: / / the second parameter of the method encapsulated by this delegate. / / Type parameter: / / T1: / / the type of the first parameter of the method encapsulated by this delegate. / T2: / / the type of the second parameter of the method encapsulated by this delegate. / TResult: / / the return value type of the method encapsulated by this delegate. / return result: / / the return value of the method encapsulated by this delegate. [TypeForwardedFrom ("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")] public delegate TResult Func (T1 arg1, T2 arg2);}
You will find that Func is actually a delegate with multiple output parameters and a return value.
3. Example
Func has at least 0 input parameters and up to 16 input parameters, which are returned generically according to the return value. There must be a return value, not void.
Func indicates that there are no input parameters, and the return value is a delegate of type int.
Func indicates that the input parameter is object and string, and the return value is a delegate of type int.
Func indicates that the input parameter is object and string, and the return value is a delegate of type int.
Func indicates that the passed-in parameter is T1MageT2recoveryT3 (generic), and the return value is a delegate of type int.
The code example is as follows:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FunDemo {class Program {static void Main (string [] args) {/ / has no parameters, as long as the return value Func fun1 = new Func (FunWithNoPara); int result1= fun1 (); Console.WriteLine (result1) Console.WriteLine ("- -"); Func fun2 = delegate {return 19;}; int result2 = fun2 (); Console.WriteLine (result2); Console.WriteLine ("- -") Func fun3 = () = > {return 3;}; int result3 = fun3 (); Console.WriteLine (result3); Console.WriteLine ("- -"); / / there is a parameter and a return value Func fun4 = new Func (FunWithPara) Int result4 = fun4 (4); Console.WriteLine ($"here is a method with a parameter and a return value, and the return value is: {result4}"); Console.WriteLine ("- -"); / / use delegate Func fun5 = delegate (int I) {return i.ToString () }; string result5 = fun5 (5); Console.WriteLine ($"here is a delegate of a parameter and a return value, and the return value is: {result5}"); Console.WriteLine ("- -") / / use anonymous delegate Func fun6 = (int I) = > {return i.ToString ();}; string result6 = fun6 (6); Console.WriteLine ($"here is an anonymous delegate with a parameter and a return value, and the return value is: {result6}") Console.WriteLine ("- -"); / / multiple input parameters Func fun7 = new Func (FunWithMultiPara); bool result7 = fun7 (2, "2"); Console.WriteLine ($"here is the method with multiple input parameters, and the return value is: {result7}") Console.WriteLine ("- -"); / / use delegate Func fun8 = delegate (int I, string s) {return i.ToString () .Equals (s)? True: false;}; bool result8 = fun8 (2, "abc"); Console.WriteLine ($"here is the delegate with multiple input parameters, and the return value is: {result8}"); Console.WriteLine ("-") / / use anonymous delegate Func fun9 = (int I, string s) = > {return i.ToString () .Equals (s)? True: false;}; bool result9 = fun9 (45, "ert"); Console.WriteLine ($"here is an anonymous delegate with multiple input parameters, and the return value is: {result9}"); Console.ReadKey ();} static int FunWithNoPara () {return 10 } static int FunWithPara (int I) {return I;} static bool FunWithMultiPara (int iJing string s) {return i.ToString () .Equals (s)? True: false;}
Running result:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.