First, background:The Internet industry, in order to reduce program maintenance, upgrade deployment risk, often the program is split into many projects, compiled into multiple DLL deployment, so when released, only need to deploy the modified DLL.
second, the question:There is a function that is used in a number of places:
Public Fun1 (a A, b b) { //code body}
Suddenly one day, some place calls need to add a parameter C C, but do not want to have any other client program changes, you can make full use of. net4.0 new optional parameter features to change:
method One: use optional parameters
Public Fun1 (a A, B b,c C = 1) {//Code body}
After the program has been modified, the local program runs flawlessly and the DLL is tested. In the test environment, inexplicable error. Because of the system layered processing, the Web site and the Web API separation, the error returned to the top, has not seen the cause of the error, checked the long time log, finally found that another DLL (not recompiled deployment) called the FUN1 function, the report found the method fun1 error. To modify the program to:
method Two: overloading
Public Fun1 (a A, b b) { fun1 (a A, b b,1);} Public Fun1 (a A, B b,c C) { //code body}
Republish the DLL and the program runs correctly.
Iii. Preliminary Conclusions:Modify the code to an optional parameter, the client program needs to be recompiled, or the client will not be able to find a method error when called. If the client program is too many, in order to avoid deploying all of the client's DLLs, you can use method two, overloaded methods.
Four, verify:Method has no default parameters: The method has an optional parameter: The client program compiles the IL:
v. Final conclusion:By the figure, the C # source code of the client program is not changed, but the compiled IL intermediate codes are changed, the function of the call passed two parameters, and the function default parameter 888 in the client program declaration, use. It can be concluded that the C # optional parameter function is actually at compile time, in the function called the client program to do the processing, the default parameters passed in.
Six, now the problem comes, everybody please thinkWhy does Microsoft want to do this? Why not at compile time, il like the following code, do a similar overload of processing it? That way, you don't have to recompile the client program.
Public Fun1 (a A, b b) { fun1 (a A, b b,1);} Public Fun1 (a A, B b,c C) { //code body}
Seven, netizens answer: Thank CYJB and other netizens of enthusiastic answer, very detailed
Landlord for the method to add a default parameter, then this new method and the previous method of the signature is completely different-the corresponding number of parameters is different, so the error can not find the method. The optional parameters in. Net are like @ Kill, which is a syntactic sugar, Just because the compiler automatically adds default parameter values to the method call, the compiler may not support this feature at all.
In fact, depending on the VS Code Analysis feature, it is risky to see that public methods use default parameters because the method is allowed to use default parameters in the Common Language Specification (CLS), but the CLS allows the compiler to ignore the values assigned to these parameters. Code written for a compiler that ignores default parameter values must explicitly provide a variable for each default parameter. To maintain the desired behavior across programming languages, you must replace the method that uses the default parameter with a method overload that provides default parameters. The complete documentation can be found in MSDN CA1026: The default parameters should not be used.
Finally, the public method is really should only extend not modify, in order to maintain good compatibility, landlord should not think of C # default parameters is just a syntax sugar, and inadvertently in the recruit. And why does Microsoft not implement the default method as multiple overloads, not unwilling, but not. For example, the following method:
public void TestMethod (int a = +, int b = $, int c = 300) {//...}
If the automatic implementation is multiple overloads, it is obviously not possible, because the type of the parameter is int, the parameter A, B, and C cannot be distinguished according to the parameter type, so Microsoft will completely abandon this scenario. and code bloat and the like, may also be within Microsoft's consideration.
C #: Traps for optional parameters [go]