C # Basic parameters (ii) array parameters, optional parameters and named parameters

Source: Internet
Author: User

The three parameters of this introduction are grammatical sugars .

  4. Array parameters

   When declaring a method, add the params keyword before the parameter. Simplified parameter invocation for added readability.
Usage:
(1) used when the parameter is an array
(2) Each method can have only one array parameter . And as the last parameter . (Only at the end, the compiler can know which arguments belong to the array parameter when the method is called)
(3) The calling method can list the values in an array, rather than passing an array object (of course, it is not wrong to pass an array object).

  Example:

1     class Program2     {3         Static voidMain (string[] args)4         {5             intsum;6             BOOLb = Add ( outSum1,2,3,4);7             if(b)8             {9 Console.WriteLine (sum);Ten             } One         } A          Public Static BOOLADD ( out intSumparams int[] a) -         { -             Try the             { -sum =0; -                 foreach(intItemincha) -                 { +Sum + =item; -                 } +                 return true; A             } at             Catch(Exception) -             { -sum =0; -                 return false; -             } -         } in}

The output is:

10

In fact, at this time Microsoft wrapped the grammar sugar for us. The compiler will have 1,2,3,4 in an int array and pass the array to the method.

  5. Optional Parameters

  Usage:
(1) Set a default value for optional parameters when declaring a party.
(2) The default value of an optional parameter must be a constant.
(3) The optional parameter must appear after all required parameters, or the compiler will error. ()
In fact, when the method is called, the compiler assigns the arguments sequentially to the parameters, and if the number of arguments is less than the formal parameter, the default value is not assigned to the form delegate.

   Example:

    Class program    {        static void Main (string[] args)        {            int i = ADD (4);            Console.WriteLine (i);            i = ADD (1, 2);            Console.WriteLine (i);        }        public static int Add (int x, int y = 5, int z = 6)        {            return x + y + z;        }    }

The output is:

159

  6. Named parameters

   Usage:
(1) When invoking a method, explicitly describes the name of the argument corresponding to the argument.
(2) You do not have to enter parameters sequentially.
(3) When calling a method, if a parameter is named, all subsequent arguments need to be named. Otherwise the compiler will make an error.

The main function of named parameters is that for some of the various methods of parameters, you can increase the readability of the code and avoid errors in the order of the parameters.

  Example:

1 class Program2     {3         Static voidMain (string[] args)4         {5             inti = ADD (5, Secondint:6);6 Console.WriteLine (i);7         }8          Public Static intADD (intFirstint,intsecondint)9         {Ten             returnFirstint +Secondint; One         } A}

C # Basic parameters (ii) array parameters, optional parameters and named parameters

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.