In C #, there are four types of method parameters:
- Value type
- Reference type
- Output type
- Array-type parameters
Value parameter:
The so-called value parameter, is the use of value to the method to pass the parameter, the compiler gives the value of the argument to make a copy, and the copy is passed to the method, the result is that the called method does not modify the value of the argument, to ensure that the actual value of the security, when the method is called, if the type of the parameter You must ensure that the type of the argument is also a value type of data.
Example:
public class myclass{public MyClass () { /// //todo: Add 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 calling method, any changes you make to the parameters in the method are reflected in the variable, and if you use the REF keyword, you must explicitly use the REF keyword when you define the method and invoke the method.
Pay attention to the use of ref!
Example:
public class myclass{public MyClass () { /// //todo: Add 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:
Using the Out keyword for reference passing, which is lifetimes with the REF keyword, differs in that the ref requires that the variable be initialized before it is passed, and if the Out keyword is used, the Out keyword must be explicitly used when the method is defined and invoked.
Example:
public class myclass{public MyClass () { /// //todo: Add 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-type parameters:
An array-type parameter is a declaration of the params keyword that specifies a method parameter that takes a parameter at a variable number of arguments.
No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.
Example:
public class myclass{public MyClass () { /// //todo: Add 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:
Other:
Parameters of the array type:
An array type is a reference type of data, so it should also be categorized as a reference type.
public class myclass{public MyClass () { /// //todo: Add 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:
Attention:
The difference between ref and out:
The arguments passed to the ref parameter must be initialized first, and the out parameter does not have to be initialized before being passed.
The parameter type of the c#--method