Wednesday 4 January 2012

Parameter

Parameters are variables associated with a method.
An in parameter may either have its value passed in from the callee to the method's environment, so that changes to the parameter by the method do not affect the value of the callee's variable,or passed in by reference, so that changes to the variables will affect the value of the callee's variable. Value types (int, double, string) are passed in "by value"  while reference types (objects) are passed in "by reference." Since this is the default for the C# compiler, it is not necessary to use .
An out parameter does not have its value copied, thus changes to the variable's value within the method's environment directly affect the value from the callee's environment. Such a variable is considered by the compiler to be unbound upon method entry, thus it is illegal to reference an out parameter before assigning it a value. It also must be assigned by the method in each valid (non-exceptional) code path through the method in order for the method to compile.
A reference parameter is similar to an out parameter, except that it is bound before the method call and it need not be assigned by the method.
A params parameter represents a variable number of parameters. If a method signature includes one, the params argument must be the last argument in the signature.
// Each pair of lines is what the definition of a method and a call of a
// method with each of the parameters types would look like.
// In param:
void MethodOne(int param1) //definition
MethodOne(variable); //call
// Out param:
void MethodTwo(out string message) //definition
MethodTwo(out variable); //call
// Reference param;
void MethodThree(ref int someFlag) //definition
MethodThree(ref theFlag) //call
// Params
void MethodFour(params string[] names) //definition
MethodFour("Matthew", "Mark", "Luke", "John"); //call