Common Function Delegate types

Source: Internet
Author: User
  1. Action (T ))
    Encapsulate a method that uses only one parameter and does not return a value.
    Structure: void method name (T obj) {// process };
    Example: using System;
    Using System. Collections. Generic;
    Class Program
    {
    Public string Name {get; set ;}
    Public void Print (Action <string> action)
    {
    Action (this. Name );
    }
    Static void Main (string [] args)
    {
    Console. WriteLine ("used in List <T> ");
    List <string> list = new List <string> {"Flash", "Past", "Lian", "Xi "};
    List. ForEach (Program. Print );
    Console. WriteLine ("custom method usage ");
    Program pro = new Program {Name = "FlashPast "};
    Pro. Print (Program. Print );
    Console. ReadLine ();
    }
    Public static void Print (string obj)
    {
    Console. WriteLine (obj );
    }
    }

  2. Decision function delegate (Predicate (T ))
    Defines a set of conditions and determines whether the specified object meets these conditions.
    Structure: bool Match <T> (T obj) {// processing} using System;
    Using System. Collections. Generic;
    Class Program
    {
    Public string Name {get; set ;}
    Static void Main (string [] args)
    {
    Console. WriteLine ("used in List <T> ");
    Program pro = new Program {Name = "Flash "};
    List <string> list = new List <string> {"Flash", "Past", "Lian", "Xi "};
    String result = list. Find (pro. Match );
    Console. WriteLine (result );
    Console. ReadLine ();
    }
    Public bool Match (string obj)
    {
    Bool result = false;
    If (obj = this. Name)
    {
    Result = true;
    }
    Return result;
    }
    }

  3. Converter (TInput, TOutput ))
    Indicates a method to convert an object from one type to another.
    Structure: TOutput Converter <TInput, TOutput> (TInput input) {// processing} using System;
    Using System. Collections. Generic;
    Class Program
    {
    Static void Main (string [] args)
    {
    Console. WriteLine ("used in List <T> ");
    List <string> stringList = new List <string> {"1", "2", "3", "4 "};
    List <int> intList = stringList. ConvertAll <int> (Program. Converter );
    IntList. ForEach (delegate (int obj) {Console. WriteLine (obj. ToString ());});
    Console. ReadLine ();
    }
    Public static int Converter (string input)
    {
    Int result;
    If (! Int. TryParse (input, out result ))
    {
    Result =-1;
    }
    Return result;
    }
    }

  4. Comparison function delegate (Comparison (T ))
    Compares two objects of the same type.
    Structure: int Comparison <T> (T x, T y) {// processing} using System;
    Using System. Collections. Generic;
    Class Program
    {
    Static void Main (string [] args)
    {
    Console. WriteLine ("used in List <T> ");
    List <int> list = new List <int> {12, 5, 46, 37, 28, 8 };
    List. ForEach (delegate (int obj) {Console. WriteLine (obj );});
    List. Sort (Program. Comparison );
    List. ForEach (delegate (int obj) {Console. WriteLine (obj );});
    Console. ReadLine ();
    }
    Public static int Comparison (int x, int y)
    {
    Return x. CompareTo (y );
    }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.