C # method for passing Parameters

Source: Internet
Author: User

In C #, there are four main ways to pass parameters to a method: value transfer, reference transfer (REF), Output transfer (out), and parameter transfer (Params ).

1. It is needless to say that value transmission is the most common method. The parameter passed in this way is only a copy of the original parameter and does not change the value of the original parameter.

2. Ref and out are similar. They do not open up new memory areas. They are directly operated on the original parameters and will naturally change the value of the original parameters. Their differences are:

  • When passing a parameter to a call method in Ref mode, the parameter must be initialized first, and the out mode does not need to be initialized;
  • You must assign a value to the parameter before calling the method.

3. Params is a way to pass a variable number of parameters, but only one method can be used in a declaration, and the Params variable cannot have any other parameters.

Example:

Declaration: public static void sampleparam (Params object [] list );

Usage: sampleparam (1, 2, 3, "hello ");

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.