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 ");