I have learned about parameters in recent days. In fact, I have learned about these parameters since I learned the basics of C language. However, I learned something carefully.
1. Name parameters and optional parameters
Named parameters and optional parameters are new features introduced in Visual C #2010. It's silly to know today.
- Optional parameter: Set the default value for the parameter when defining a method. When calling this method, you can omit specifying real parameters for certain parameters.
- Name parameters: when calling a method, specify the parameter name to specify the real parameter for a specific parameter. This parameter is used to associate the form parameter with the real parameter based on the name, rather than the parameter location in the list.
For example:
Class Program
{
Static void main (string [] ARGs)
{
// Optional parameter
Employee. displayinfo (24 ," Program Clerk "); // equivalent to employee. displayinfo (24," programmer ", 0," unknown ", default (datetime ));
Employee. displayinfo (25, "English related", 9500); // equivalent to employee. displayinfo (25, "English related", 9500, "unknown", default (datetime ));
// Name Parameters
// Equivalent to employee. displayinfo (30, "Senior Software Engineer", 20000, "yathur", datetime. Now );
Employee. displayinfo (30, job: "Senior Software Engineer", salary: 20000, name: "yathur", DT: datetime. Now );
Employee. displayinfo (age: 25, job: "instructor ");
Console. Read ();
}
}
Public class employee
{
Public static void displayinfo (INT age, string job, int salary = default (INT), string name = "unknown", datetime dt = default (datetime ))
{
Console. writeline ("name:" + name + ", age:" + age + ", position:" + job + ", monthly salary" + salary + "" + dt );
}
} Running result:
In intelligent sensing, the optional parameter is square brackets.
With the name parameters, you can easily follow the name of the parameter. You do not need to remember or find the order of the parameters in the list of parameters of the called method. With optional parameters, if a method that contains a large number of parameters contains default values, we can only specify real parameters for meaningful parameters, which is much easier to use.
Rules:
- Parameters without default values can also be passed by name.
Employee. displayinfo (age: 25, job: "instructor ");
- The order of naming parameters can be changed, but the parameters with default values must be placed after the parameters without default values; otherwise, a compilation error may occur.
NOTE: If an array parameter exists in the parameter, this rule is not valid. Because the array parameter has no default value, it must be placed at the end of the parameter list.
- The default value must be a value that can be determined during compilation. For example, the primitive type, enumeration type, and reference type that can be set to null. You can use the default and new keywords to set the default value to the corresponding zero value.
- If you cannot set the default value for a parameter that contains the ref or out keyword, a compilation error occurs. Because these parameters are difficult to set meaningful default values
Ii. Pass Parameter-out and ref in Reference Mode
CLR assumes that the parameters of a method are carried out by passing values, but it allows passing parameters by passing references. C # You can pass a pointer to the instance through the out and ref keywords, instead of the parameter value.
Differences Between out and Ref:
- Out: you do not need to initialize the parameter value before calling. The parameter value cannot be read inside the called method. You must assign a value to the parameter before returning the value.
- Ref: The parameter value must be initialized before the call. The called method can read or write the value of the parameter. The parameter value can not be assigned before the return value (regardless of the actual meaning of the program ).
1.
Use out for Value Type
And ref
Running result:
Corresponding il metadata:
2.
Reference Type use out
And ref
When a method is called, if the parameter type is a base class of the real parameter type, implicit conversion is generally performed. For example:
Public static void swap (Object A, object B)
{
Object T =;
A = B;
B = T;
}
String A = "AAA", B = "BBB ";
Swap (A, B );
Console. writeline ("A =" + A + "B =" + B );
However, the running result is not what we want, because the method transmits parameters by default, and the original value will not change after the call.
When passing parameters to a method by reference, you must ensure that the variable type is consistent with the type in the method signature, rather than automatic implicit conversion. Otherwise, a compilation error occurs. For example:
Public static void swap (ref object A, ref object B)
{
Object T =;
A = B;
B = T;
}
To achieve the desired effect, we can useGeneric.
Public static void swap <t> (ref t a, ref t B)
{
T =;
A = B;
B = T;
}
Static void main (string [] ARGs)
{
String A = "AAA", B = "BBB ";
Swap (Ref A, ref B );
Console. writeline ("A =" + A + "B =" + B );
Console. Read ();
}
3.
Heavy Load
In C #, you can use the out or ref keyword for method overloading. For example, the following example is valid:
However, if the two methods have the same name and parameter, only the out and ref keywords cannot be considered as overload.
Iii. variable quantity parameter transfer-Params
Sometimes we want the number of method parameters to be variable. Params helps us implement this idea.
Instance
If the Params keyword is not used, we need to use the ① call method. When the Params keyword is added, we can use ② ③ To make the call method more intuitive. It achieves the effect of passing variable quantity parameters.
Note:
- The Params keyword parameter can only be placed at the end of the parameter list. If you change the two parameter positions in the preceding example, a compilation error occurs.
- The Params keyword can only identify any type of one-dimensional array.
- The Params parameter can be a null value or an array with a length of 0. For example, ④ in the above example.
- The Params method will actually cause some performance losses, such as allocating memory to arrays and initializing arrays on the heap. Therefore, do not use too many variable length parameters.