Four types of method parameters in C #

Source: Internet
Author: User

 

There are four types of method parameters in C:

-Value parameter: it does not contain any modifier. The parameters in the method are copies of real parameters. Changes in the parameters do not affect the values of real parameters in the memory. Real parameters are safe.

-Reference parameter: declared with a ref modifier. The passed parameter is actually a pointer to the real parameter. Therefore, operations in the method are directly performed on the real parameter, rather than copying a value. You can use this method to transmit parameters in both directions during method calling; to use parameters in Ref mode, the ref keyword must be explicitly specified in the method declaration and method call, and the real variable must be initialized before being passed to the method.

-Output parameter: declare with the out modifier. Similar to ref, it directly operates on real parameters. The out keyword must be explicitly specified during method declaration and method call. The out parameter declaration method does not require variables to be initialized before being passed to the method, because it only serves as the output. However, the out parameter must be assigned a value before the method is returned.

-Array parameters: declared with Params modifier. The Params keyword is used to declare the variable length parameter list. The method declaration can only contain one Params parameter.

Using system;

Class Test

{

Static void F (Params int [] ARGs)

{

Console. writeline ("array contains {0} elements:", argS. Length );

Foreach (int I in ARGs)

Console. Write ("{0}", I );

Console. writeline ();

}

Public static void main ()

{

Int [] A = {1, 2, 3 };

F ();

F (10, 20, 30, 40 );

F ();

}

}

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.