TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# delegate Keyword

Use delegate methods. Understand lambda expressions and types such as Predicate.
Delegate. This keyword (delegate) is used to create method objects. With the Invoke() method, we can call a delegate as a method.KeywordsEvent
Notes, higher-order. We can pass the delegate as an argument to another method. The function is an object—a delegate is a higher-order procedure.
Example. This program declares a delegate type D that receives a string, and returns no value. In Main, we create an instance of type D.

Lambda: We specify the method body with a lambda expression. We can use lambda expressions with delegate types.

Lambda

Then: We call the delegate D method implementation with the argument "cat" using the Invoke method.

Tip: This program uses the "delegate" keyword. But many programs use higher-order procedures with just lambda expressions.

C# program that uses delegate using System; class Program { delegate void D(string value); static void Main() { // ... Specify delegate with lambda expression. D d = v => Console.WriteLine(v); // ... Invoke delegate. d.Invoke("cat"); } } Output cat
Example 2. Next, we build upon the delegate concept. We declare a delegate type UppercaseDelegate. It receives a string, and returns a string.

Main: We create instances of this delegate Type. We need to declare a delegate type before creating instances of it.

Important: When you construct instances of the delegate type, the target method must have the same return value and arguments.

WriteOutput: The second argument is of type UppercaseDelegate. When called, WriteOutput invokes the function argument.

Note: The target method is specified as the sole argument. It is a method name, not a variable.

C# program that uses UppercaseDelegate using System; class Program { delegate string UppercaseDelegate(string input); static string UppercaseFirst(string input) { char[] buffer = input.ToCharArray(); buffer[0] = char.ToUpper(buffer[0]); return new string(buffer); } static string UppercaseLast(string input) { char[] buffer = input.ToCharArray(); buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]); return new string(buffer); } static string UppercaseAll(string input) { return input.ToUpper(); } static void WriteOutput(string input, UppercaseDelegate del) { Console.WriteLine("Your string before: {0}", input); Console.WriteLine("Your string after: {0}", del(input)); } static void Main() { // Wrap the methods inside delegate instances and pass to the method. WriteOutput("Codex", new UppercaseDelegate(UppercaseFirst)); WriteOutput("Codex", new UppercaseDelegate(UppercaseLast)); WriteOutput("Codex", new UppercaseDelegate(UppercaseAll)); } } Output Your string before: Codex Your string after: Perls Your string before: Codex Your string after: perlS Your string before: Codex Your string after: PERLS
Anonymous function. Anonymous functions include lambda expressions. You can pass a lambda expression as the implementation of the delegate.Anonymous Functions

Tip: You can construct the UppercaseDelegate by specifying a lambda expression inside the constructor itself.

Info: This is functionally equivalent to using a static method with the same return value and argument.

C# program that uses lambda expression, delegate using System; class Program { // ... // ... (Paste other methods here) // ... static void Main() { WriteOutput("Codex", new UppercaseDelegate(x => x.ToUpper())); } } Output PERLS
Lambda. The syntax for delegate functions can be complex. Lambda expressions provide a simple and terse way of specifying functions.

Tip: Lambdas use the => syntax to separate formal parameters and the method body.

Info: Whenever you see a method that receives a Predicate, Action, Func or Comparison, a lambda can be passed.

PredicateActionFuncComparison
Events. Events allow you to specify that when external event occurs, such as a mouse click, a delegate method should always be executed.

But: You can define custom events in any program. They are occasionally useful in complex programs.

LINQ. In LINQ extensions, delegate methods are extensively used. Even query expressions are implemented with methods that receive delegates.LINQ
A summary. Delegate syntax is at first confusing. Once you understand the instantiation model and type system, it offers immense benefits to certain programs.
With the type system, the language offers the ability to specify actions as data. Complicated (and possibly hard-to-understand) programs can be written.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf