C # 's action/func/out/ref/params/type parameter constraints

Source: Internet
Author: User
C # 's action/func/out/ref/params/type parameter constraints

Use of Action:

Using System;
Using System.Collections.Generic;

Class Program {
    ///<summary>
    ///output string
    ///</summary>
    static void Printstring ()
    {
        Console.WriteLine ("Hello World.");
    }

    <summary>
    ///Output integer
    ///</summary>
    ///<param name= "i" > Integer </param>
    static void Printint (int i)
    {
        Console.WriteLine (i);
    }

    static void Main (string[] args)
    {
        Action a = printstring;//action is a system-built (predefined) delegate type that can point to a method that has no return value and no parameters
        action<int> B = printint;//defines a delegate type that can point to a method

        a ()
        that has no return value and has an int parameter. B (m);

        Console.readkey ();
           
    }




Usage of func:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks;
    Class Program {static int Test1 () {return 1;
        static bool Test (int x) {if (x== 0) {return true;
        else {return false;
        } static int Test2 (String str) {Console.WriteLine (str);
    return 100;
    static int Test3 (int i, int j) {return i + J;

        } static void FunctionTest (bool x) {if (x) {Console.WriteLine ("This is 0!");
        else {Console.WriteLine ("This is non 0!"); } static void Main (string[] args) {func<int> a = generic type in Test1;//func is the return value type of the method Fu nc<string, int> b = Test2;//func can be followed by many types, the last type is the return value type, the preceding type is the parameter type, and the parameter type must correspond to the parameter type of the method pointed to func<int, int, Int> C = Test3;//func must specify a return value class afterType, the parameter type can have 0-16, write the parameter type first, the last one is the return value type func<int,bool> f = Test;
        Console.WriteLine (A ());
        Console.WriteLine (b ("Husheng"));

        Console.WriteLine (c (123, 456));
        Program.functiontest (f (0));

        Program.functiontest (f (1));
    Console.readkey ();
 }
}



About the callback function of understanding: like you buy something online, but this thing is temporarily out of stock, so you will leave your phone number online, when the arrival of the site will automatically send you a message to remind the arrival. So, your call is equivalent to a callback function, and the result of the callback function is that you receive the "Arrival" notice. Then, according to the notice, you decide whether you want to buy it or not.


The REF keyword causes the argument to be passed by reference. The effect is that when control passes back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.


The Out keyword causes the arguments to be passed by reference. This is similar to the REF keyword, except that ref requires that the variable be initialized before it is passed.
Both method definitions and invocation methods must explicitly use the Out keyword.
property is not a variable and cannot be passed as an out parameter.


Params type parameters are used with arrays to implement an indefinite length of array parameters.


Type parameter constraints,. NET supports the following five types of parameter constraints:
where T:struct | T must be a struct type
where T:class | T must be a class (class) type and cannot be a struct (structure) type
where T:new () | T must have a parameterless constructor
where T:nameofbaseclass | T must inherit a class named Nameofbaseclass
where T:nameofinterface | T must implement an interface named Nameofinterface


====================================================================================

End.

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.