[C #] Precautions for optional parameters and constants across modules

Source: Internet
Author: User
Tags mscorlib

[C #] Precautions for optional parameters and constants across modules
Assume that a DLL has such a class:

1 // Lib.dll2 public class Lib3 {4     public const string VERSION = "1.0";5     public static void PrintVersion(string version = "1.0")6     {7         Console.WriteLine(version);8     }9 }

 

Then there is such a caller:
// Program.execlass Program{    static void Main()    {        Console.WriteLine(Lib.VERSION);        Lib.PrintVersion();        Console.Read();    }}

 

The running result of Program.exe is obvious: later than 1.01.0, the Lib version has been updated:
// Lib.dllpublic class Lib{    public const string VERSION = "2.0";    public static void PrintVersion(string version = "2.0")    {        Console.WriteLine(version);    }} 

 

Summary, result: 1.01.0re-compile program.exe and run it again: 2.02.0 finds the problem. The caller must re-compile to ensure that the values of optional parameters and constants are the latest. The reason is as follows:
 1 .method private hidebysig static  2     void Main () cil managed  3 { 4     // Method begins at RVA 0x2050 5     // Code size 30 (0x1e) 6     .maxstack 8 7     .entrypoint 8  9     IL_0000: ldstr "1.0"10     IL_0005: call void [mscorlib]System.Console::WriteLine(string)11     IL_000a: ldstr "1.0"12     IL_000f: call void CsConsole.Program/Lib::PrintVersion(string)13     IL_0014: call int32 [mscorlib]System.Console::Read()14     IL_0019: pop15     IL_001a: ret16 }

 

This is the first time that the Program is compiled. main method IL, Lib. the VERSION is fully compiled into the literal "1.0" (10th rows), while the "11th" of the 1.0 rows comes from (at the first compilation) Lib. dll metadata. Obviously, no matter how the Lib code is updated, as long as the Program is not re-compiled, the two values here cannot be updated, and the actual production environment often cannot ensure that the caller will be re-compiled. I understand why this compilation is necessary. The constant value stays in the constant pool. It is obviously better to take the value directly from the constant pool through the class member. The optional parameter implementation method is to judge and assign values in advance during compilation, saving the runtime time. Solution: for optional parameters, the recommended method for CLR via C # is as follows:
 1 public static void PrintVersion(string version = null)2 {3     if (version == null)4     {5         version = "1.0";6     }7     Console.WriteLine(version);8 }

 

Obviously, the behavior of this code has changed compared with the previous one, but it can solve the value update problem in most cases, but it also brings about the running timeliness rate problem. For constants, we can see from my example that the values of version numbers should not be defined as constants and readonly can be used for the purpose. For a real constant, its value should not be changed easily. Finally, although Java does not have const, static final has the same performance. Pay attention to this point as well.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.