C # method parameter type

Source: Internet
Author: User

In C #, there are four parameter types of methods:

  • Value Type
  • Reference Type
  • Output type
  • Array parameters

 

Value parameter:

The so-called value parameter is to use the value to pass a parameter to the method, compile the program to make a copy of the value of the real parameter, and pass this copy to the method, the result is that the called method does not modify the value of the real parameter, ensuring the security of the actual value. When calling the method, if the value type of the parameter type is, make sure that the real parameter type is also the value type data.

Example:

Public class myclass {public myclass () {// todo: add the constructor logic here //} public void changevalue (string value) {value = "value is changed! ";}}----------------------------------- String value =" value "; response. write (Value + "<br/>"); myclass MC = new myclass (); MC. changevalue (value); response. write (value );

 

Effect:

Reference parameters:

You can use the ref keyword to pass Parameters by reference. when you need to pass back the call method, any changes made to the parameter in the method will be reflected in this variable. If you use the ref keyword, the ref keyword must be explicitly used when the method is defined and called.

Note the use of Ref!

Example:

Public class myclass {public myclass () {// todo: add the constructor logic here //} public void changevalue (ref string value) {value = "value is changed! ";}}----------------------------- String value =" value "; response. write (Value + "<br/>"); myclass MC = new myclass (); MC. changevalue (ref value); response. write (value );

 

Effect:

Output parameters:

The out keyword is used for reference transfer, which is very tiring with the ref keyword. The difference is that the ref requires that the variable must be initialized before being passed. If the out keyword is used, the out keyword must be explicitly used for method definition and call.

Example:

Public class myclass {public myclass () {// todo: add the constructor logic here //} public void changevalue (out string value) {value = "value is changed! ";}}------------------------------- String value; myclass MC = new myclass (); Mc. changevalue (out value); response. Write (value );

 

Effect:

Array parameters:

Array parameters are the Params declaration keywords used to specify the parameter method parameters when the number of parameters is variable.

After the Params keyword in the method declaration, no other parameters are allowed, and only one Params keyword is allowed in the method declaration.

Example:

Public class myclass {public myclass () {// todo: add the constructor logic here //} public void changevalue (Params string [] value) {foreach (string s in value) {httpcontext. current. response. write (S + "<br/>") ;}}------------------------------------------ string value1 = "value1"; string value2 = "value2"; myclass MC = new myclass (); MC. changevalue (value1, value2 );

 

Effect:

 

In addition:

Parameters of the array type:

The array type is the data of the reference type, so it should also be classified as the reference type.

Public class myclass {public myclass () {// todo: add the constructor logic here //} public void changevalue (string [] value) {value [0] = "this is value0, changed! ";}}---------------------------------------------- String [] value = {" value1 "," value2 "}; response. write (value [0] + "<br/>"); myclass MC = new myclass (); MC. changevalue (value); response. write (value [0] + "<br/> ");

 

 

Effect:

 

Note:

Differences between ref and out: (ref initialization)

Parameters passed to the ref parameter must be initialized first, and out parameters do not need to be initialized before being passed.

Http://www.cnblogs.com/oneword/archive/2009/08/19/1549464.html

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.