{"id":1516,"date":"2025-03-10T00:38:24","date_gmt":"2025-03-09T16:38:24","guid":{"rendered":"http:\/\/codermr.com\/?p=1516"},"modified":"2025-03-10T00:51:14","modified_gmt":"2025-03-09T16:51:14","slug":"c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/","title":{"rendered":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction to the C# delegates<\/h2>\n\n\n\n<p>In C#, delegates are types that represent references to methods with a particular parameter list and return type.<\/p>\n\n\n\n<p>To define a delegate, you use the&nbsp;<code>delegate<\/code>&nbsp;keyword and specify the method signature. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delegate void Greeting(string message);<\/code><\/pre>\n\n\n\n<p>In this example, we define the&nbsp;<code>Greeting<\/code>&nbsp;delegate type that can reference any method which accepts a string argument and returns&nbsp;<code>void<\/code>.<\/p>\n\n\n\n<p>Since the&nbsp;<code>Greeting<\/code>&nbsp;is a delegate type, you can declare it outside a class like other classes.<\/p>\n\n\n\n<p>The following defines the&nbsp;<code>SayHi()<\/code>&nbsp;method for the&nbsp;<code>Program<\/code>&nbsp;class, which has the same signature as the&nbsp;<code>Greeting<\/code>&nbsp;delegate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Program\n{\n    static void SayHi(string name)\n    {\n        Console.WriteLine($\"Hi {name}\");\n    }\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>To call the&nbsp;<code>SayHi()<\/code>&nbsp;method via the&nbsp;<code>Greeting<\/code>&nbsp;delegate, you create an instance of the&nbsp;<code>Greeting<\/code>&nbsp;delegate with the&nbsp;<code>SayHi<\/code>&nbsp;method as an argument and call the&nbsp;<code>Invoke()<\/code>&nbsp;method of the delegate instance like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Greeting greeting = new Greeting(SayHi);\ngreeting.Invoke(\"John\");<\/code><\/pre>\n\n\n\n<p>In this syntax, the&nbsp;<code>greeting<\/code>&nbsp;is an instance of the&nbsp;<code>Greeting<\/code>&nbsp;delegate type. The&nbsp;<code>greeting<\/code>&nbsp;delegate holds a reference to the&nbsp;<code>SayHi()<\/code>&nbsp;method. Internally, the&nbsp;<code>greeting<\/code>&nbsp;delegate maintains an invocation list that has a reference to the&nbsp;<code>SayHi()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>When you call the&nbsp;<code>Invoke()<\/code>&nbsp;method of the&nbsp;<code>greeting<\/code>&nbsp;delegate, C# will call the&nbsp;<code>SayHi()<\/code>&nbsp;method with the same argument. Therefore, the following statements are functionally equivalent:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>greeting.Invoke(\"John\");<\/code><\/pre>\n\n\n\n<p>And:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SayHi(\"John\");<\/code><\/pre>\n\n\n\n<p>C# provides you with a shorter way to create a new instance of the&nbsp;<code>Greeting<\/code>&nbsp;delegate by assigning the&nbsp;<code>SayHi<\/code>&nbsp;method to a delegate variable and calling the referenced method via the delegate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Greeting greeting = SayHi;\ngreeting(\"John\");<\/code><\/pre>\n\n\n\n<p>Note that you assign the method name&nbsp;<code>SayHi<\/code>&nbsp;without parentheses&nbsp;<code>()<\/code>&nbsp;to the delegate variable.<\/p>\n\n\n\n<p>Put it all together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delegate void Greeting(string message);\n\nclass Program\n{\n    static void SayHi(string name)\n    {\n        Console.WriteLine($\"Hi {name}\");\n    }\n\n    static void Main(string&#91;] args)\n    {\n        Greeting greeting = SayHi;\n        greeting(\"John\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Hi John<\/code><\/pre>\n\n\n\n<p>If you have C++ background, the fastest way for you to understand delegates is to think of them as function pointers. However, a delegate is fully object-oriented in C#. And unlike C++ function pointers, delegates encapsulate both an object instance and a method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why delegates<\/h2>\n\n\n\n<p>Since you can directly call the&nbsp;<code>SayHi()<\/code>&nbsp;method, you don\u2019t need to call it via the delegate. The question is why do you need a delegate?<\/p>\n\n\n\n<p>Because delegates hold references to methods, you can pass methods as arguments to other methods via the delegates. Therefore, delegates are ideal for defining&nbsp;<strong>callback methods<\/strong>.<\/p>\n\n\n\n<p>Suppose you want to define a method that filters a list of integers based on the result of another method. To do that, you can use a delegate.<\/p>\n\n\n\n<p>First, define a delegate type that accepts an integer and returns a boolean value:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>delegate bool Callback(int x);<\/code><\/pre>\n\n\n\n<p>Second, define the&nbsp;<code>Filter()<\/code>&nbsp;method that accepts a list of integers and an instance of the&nbsp;<code>Callback<\/code>. If an integer causes the callback to return&nbsp;<code>true<\/code>, the result of the&nbsp;<code>Filter()<\/code>&nbsp;method will include that integer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static List&lt;int> Filter(List&lt;int> numbers, Callback callback)\n{\n    var results = new List&lt;int>();\n\n    foreach (var number in numbers)\n    {\n        if (callback(number))\n        {\n            results.Add(number);\n        }\n    }\n\n    return results;\n}<\/code><\/pre>\n\n\n\n<p>Third, define the&nbsp;<code>isOdd()<\/code>&nbsp;method that returns&nbsp;<code>true<\/code>&nbsp;if a number is odd and the&nbsp;<code>isEven()<\/code>&nbsp;method that returns&nbsp;<code>true<\/code>&nbsp;if a number is even:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static bool IsOdd(int x) => x % 2 != 0;\nstatic bool IsEven(int x) => x % 2 == 0;<\/code><\/pre>\n\n\n\n<p>Fourth, call the&nbsp;<code>Filter()<\/code>&nbsp;method and pass an instance of the&nbsp;<code>Callback<\/code>&nbsp;delegate that references the&nbsp;<code>IsEven()<\/code>&nbsp;method. The&nbsp;<code>Filter()<\/code>&nbsp;method returns a list of even integer numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var numbers = new List&lt;int> { 1, 2, 3, 4, 5 };\n\nvar evenNumbers = Filter(numbers, IsEven);\n\nConsole.WriteLine(\"Even numbers:\");\nforeach (var number in evenNumbers)\n{\n    Console.WriteLine($\"{number}\");\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Even numbers:\n2\n4<\/code><\/pre>\n\n\n\n<p>Fifth, call the&nbsp;<code>Filter()<\/code>&nbsp;method and pass an instance of the&nbsp;<code>Callback<\/code>&nbsp;delegate that references the&nbsp;<code>isOdd()<\/code>&nbsp;method. The&nbsp;<code>Filter()<\/code>&nbsp;method returns a list of odd integer numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var numbers = new List&lt;int> { 1, 2, 3, 4, 5 };\n\nvar oddNumbers = Filter(numbers, IsOdd);\n\nConsole.WriteLine(\"Odd numbers:\");\nforeach (var number in oddNumbers)\n{\n    Console.WriteLine($\"{number}\");\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Odd numbers:\n1\n3\n5<\/code><\/pre>\n\n\n\n<p>Put it all together:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Program\n{\n    delegate bool Callback(int x);\n\n    static List&lt;int> Filter(List&lt;int> numbers, Callback callback)\n    {\n        var results = new List&lt;int>();\n\n        foreach (var number in numbers)\n        {\n            if (callback(number))\n            {\n                results.Add(number);\n            }\n        }\n\n        return results;\n    }\n\n    static bool IsOdd(int x) => x % 2 != 0;\n    static bool IsEven(int x) => x % 2 == 0;\n\n    static void Main(string&#91;] args)\n    {\n        var numbers = new List&lt;int> { 1, 2, 3, 4, 5 };\n\n        var evenNumbers = Filter(numbers, IsEven);\n        Console.WriteLine(\"Even numbers:\");\n        foreach (var number in evenNumbers)\n        {\n            Console.WriteLine($\"{number}\");\n        }\n\n\n        var oddNumbers = Filter(numbers, IsOdd);\n        Console.WriteLine(\"Odd numbers:\");\n        foreach (var number in oddNumbers)\n        {\n            Console.WriteLine($\"{number}\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>By using a delegate as a callback, you can pass a method as an argument to another method. In this example, the&nbsp;<code>Filter()<\/code>&nbsp;method is very dynamic that can accept any method for filtering the integer list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding methods to a delegate<\/h2>\n\n\n\n<p>A delegate can hold references to multiple methods. In this case, the delegate is called a&nbsp;<strong>multicast delegate<\/strong>.<\/p>\n\n\n\n<p>To add a method to a delegate, you use the&nbsp;<code>+=<\/code>&nbsp;operator. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delegate void Greeting(string message);\n\nclass Program\n{\n    static void SayHi(string name) => Console.WriteLine($\"Hi {name}\");\n\n    static void SayBye(string name) => Console.WriteLine($\"Bye {name}\");\n   \n    static void Main(string&#91;] args)\n    {\n        Greeting greeting = SayHi;\n        greeting += SayBye;\n        greeting(\"John\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi John\nBye John<\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define the&nbsp;<code>Greeting<\/code>&nbsp;delegate type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">delegate void Greeting(string message);<\/pre>\n\n\n\n<p>Next, define two static methods&nbsp;<code>SayHi()<\/code>&nbsp;and&nbsp;<code>SayBye()<\/code>&nbsp;in the&nbsp;<code>Program<\/code>&nbsp;class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static void SayHi(string name) => Console.WriteLine($\"Hi {name}\");\nstatic void SayBye(string name) => Console.WriteLine($\"Bye {name}\");<\/code><\/pre>\n\n\n\n<p>Then, create a new instance of the&nbsp;<code>Greeting<\/code>&nbsp;delegate type and assign the&nbsp;<code>SayHi<\/code>&nbsp;method to greeting variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Greeting greeting = SayHi;<\/code><\/pre>\n\n\n\n<p>After that, add a new method to the invocation list of the&nbsp;<code>greeting<\/code>&nbsp;delegate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>greeting += SayBye;<\/code><\/pre>\n\n\n\n<p>Finally, call the methods in the invocation list of the greeting delegate:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>greeting(\"John\");<\/code><\/pre>\n\n\n\n<p>This statement invokes the&nbsp;<code>SayHi()<\/code>&nbsp;and&nbsp;<code>SayBye()<\/code>&nbsp;method in the invocation list of the greeting method. It\u2019s important to note that the delegate may call the methods in its invocation list in any order. Therefore, you should not rely on the order of the methods.<\/p>\n\n\n\n<p>Delegates are immutable. It means that a delegate cannot be changed once it is created. Therefore, the following creates a new delegate and assigns it to the greeting variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>greeting += SayBye;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing a method from a delegate<\/h2>\n\n\n\n<p>To remove a method from a delegate, you use the&nbsp;<code>-=<\/code>&nbsp;operator. Note that C# will issue an error if you attempt to call a delegate with an empty invocation list.<\/p>\n\n\n\n<p>The following example illustrates how to remove a method from the invocation list of a delegate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delegate void Greeting(string message);\n\nclass Program\n{\n    static void SayHi(string name) => Console.WriteLine($\"Hi {name}\");\n\n    static void SayBye(string name) => Console.WriteLine($\"Bye {name}\");\n\n    static void Say(string message) => Console.WriteLine(message);\n\n    static void Main(string&#91;] args)\n    {\n        Greeting greeting = SayHi;\n        greeting += Say;\n        greeting += SayBye;\n\n        greeting -= SayHi;\n\n        greeting(\"John\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>John\nBye John<\/code><\/pre>\n\n\n\n<p>In this example, before calling the methods, we remove the&nbsp;<code>SayHi<\/code>&nbsp;method from the invocation list of the greeting delegate:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>greeting -= SayHi;<\/code><\/pre>\n\n\n\n<p>If you remove all the methods from the invocation list of a delegate, the delegate will be null. To invoke the delegate with a null check, you can use a null conditional operator like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>greeting?.Invoke(\"John\");<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul>\n<li>A delegate is a type that references methods with a particular parameter list and return type.<\/li>\n\n\n\n<li>Use a delegate as a callback to pass a method as an argument to another method.<\/li>\n\n\n\n<li>Delegates are immutable.<\/li>\n\n\n\n<li>Use&nbsp;<code>+=<\/code>&nbsp;operator to add a method to the invocation list of a delegate.<\/li>\n\n\n\n<li>Use&nbsp;<code>-=<\/code>&nbsp;operator to remove a method from the invocation list of a delegate.<\/li>\n<\/ul>\n\n\n\n<p>\u53c2\uff1a<a href=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-delegate\/\">https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-delegate\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to the C# delegates In C#, delegates are t&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[60],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1alambda\u3001delegates\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Introduction to the C# delegates In C#, delegates are t...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1alambda\u3001delegates\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-09T16:38:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-09T16:51:14+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/csharp1-Lambda\u3001Delegates-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"491\" \/>\n\t<meta property=\"og:image:height\" content=\"468\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"\u7801\u5148\u751f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mrcode2021\" \/>\n<meta name=\"twitter:site\" content=\"@mrcode2021\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u7801\u5148\u751f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate\",\"datePublished\":\"2025-03-09T16:38:24+00:00\",\"dateModified\":\"2025-03-09T16:51:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/\"},\"wordCount\":968,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"csharp\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/\",\"name\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2025-03-09T16:38:24+00:00\",\"dateModified\":\"2025-03-09T16:51:14+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/codermr.com\/#website\",\"url\":\"http:\/\/codermr.com\/\",\"name\":\"\u7801\u5148\u751f\u7684\u535a\u5ba2\",\"description\":\"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP\",\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/codermr.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\",\"name\":\"\u7801\u5148\u751f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"contentUrl\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"width\":400,\"height\":400,\"caption\":\"\u7801\u5148\u751f\"},\"logo\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/codermr.com\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021\"],\"url\":\"http:\/\/codermr.com\/index.php\/author\/coderma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1alambda\u3001delegates\/","og_locale":"zh_CN","og_type":"article","og_title":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"Introduction to the C# delegates In C#, delegates are t...","og_url":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1alambda\u3001delegates\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2025-03-09T16:38:24+00:00","article_modified_time":"2025-03-09T16:51:14+00:00","og_image":[{"width":491,"height":468,"url":"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/csharp1-Lambda\u3001Delegates-1.png","type":"image\/png"}],"author":"\u7801\u5148\u751f","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mrcode2021","twitter_site":"@mrcode2021","twitter_misc":{"\u4f5c\u8005":"\u7801\u5148\u751f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate","datePublished":"2025-03-09T16:38:24+00:00","dateModified":"2025-03-09T16:51:14+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/"},"wordCount":968,"commentCount":0,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["csharp"],"articleSection":["C#"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/","url":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/","name":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2025-03-09T16:38:24+00:00","dateModified":"2025-03-09T16:51:14+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/10\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e5%85%ad%ef%bc%9alambda%e3%80%81delegates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u516d\uff1aLambda\u3001Delegate"}]},{"@type":"WebSite","@id":"http:\/\/codermr.com\/#website","url":"http:\/\/codermr.com\/","name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","description":"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP","publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/codermr.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf","name":"\u7801\u5148\u751f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/","url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","contentUrl":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","width":400,"height":400,"caption":"\u7801\u5148\u751f"},"logo":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/codermr.com","https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021"],"url":"http:\/\/codermr.com\/index.php\/author\/coderma\/"}]}},"_links":{"self":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1516"}],"collection":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/comments?post=1516"}],"version-history":[{"count":10,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1516\/revisions"}],"predecessor-version":[{"id":1532,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1516\/revisions\/1532"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media\/1522"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=1516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1516"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}