C # keyword Params ref out

Source: Internet
Author: User

3,Params

ParamsYou can specify the parameter when the number of parameters is variable.

  1. In the method declarationParamsNo other parameters are allowed after the keyword, and only one parameter is allowed in the method declaration.
    ParamsKeyword.

The literal meaning is difficult to understand, so it is useful to look at the example.

Using system;

 

Class app

{

Public static voiduseparams (Params object [] list)

{

For (INT I = 0; I <list. length; I ++)

{

Console. writeline (list [I]);

}

}

 

Static void main ()

{

//
The general practice is to first construct an object array and then use this array as a parameter of the method.

Object [] arr = newobject [3] {100, 'A', "keywords "};

Useparams (ARR );

 

//
After using the Params modifier parameters, we can directly use a group of objects as parameters.

// Of course, this set of parameters must meet the parameter requirements of the called Method

Useparams (100, 'A', "keywords ");

 

Console. Read ();

}

}

 

Keywords: ref and out

 

  1. To use the ref parameter, both the method definition and the call method must be explicitly used.RefKeyword.
  2. Parameters passed to the ref parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed.

// Keywords_ref.cs

Using system;

Class app

{

Public static void useref (ref int I)

{

I + = 100;

Console. writeline ("I = {0}", I );

}

Static void main ()

{

Int I = 10;

// View the value before calling the Method

Console. writeline ("before themethod calling: I = {0}", I );

Useref (Ref I );

// View the value after the method is called

Console. writeline ("after themethod calling: I = {0}", I );

Console. Read ();

}

}

/*

Console output:

Beforethe method calling: I = 10

I = 110.

Afterthe method calling: I = 110

*/

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.