What is the idea of ​​providing a private constant for use as default parameters for public methods?

To my surprise, this one compiles and runs:

class Program { static void Main() { DoSomething(); } private const int DefaultValue = 2; // <-- Here, private. public static void DoSomething(int value = DefaultValue) { Console.WriteLine(value); } } 

The method is publicly available, while the default value "redirects" to a constant personal variable.

My question is:

What is the idea of ​​this concept?

My understanding (to this day) was that something publicly available could only be used if all the other "link" elements were also publicly available.

Update:

I just ILSpy decompiled my class to find:

 static class Program { private static void Main() { Program.DoSomething(2); } private const int DefaultValue = 2; public static void DoSomething(int value = 2) { Console.WriteLine(value); } } 

So, if a private constant as the default parameter is executed, for example, a library, the library user still sees the default parameter.

+6
source share
4 answers

The name DefaultValue is private, but the number 2 is still the number 2 .

Since DefaultValue is private, we cannot access Program.DefaultValue from outside Program . Presumably, we would not particularly want to.

And since we still decided to define DefaultValue , this is apparently what we care about when we work on how the Program works.

So, when we come to the definition of the default value for DoSomething , presumably there is some logical reason why the required value there will be the same as the value of DefaultValue .

And as such, it may be useful to use this constant there, for the same reasons that we find the constants useful anywhere.

And since DefaultValue is just a Program specific way of saying 2 , there is no real reason why we cannot.

Of course, the metadata will reflect this as 2 , and not (without meaning for the external) DefaultValue , but then it will be executed if const was public anyway (the metadata about the default values ​​give only the value, and not regardless of whether it with any defined constants).

Therefore, there are no shortcomings.

So, given that:

  • There is an advantage for the performer.
  • There is no flaw for the user just using literal 2 .
  • Preventing this would be to introduce a special rule as an exception to the rule in which certain constants can be used wherever a constant value from a literal can be.

Why not?

+6
source

What is the idea of ​​this "concept"?

The idea is that since the value of const static and never changes, you can use it as the default value for optional method parameters, just as you can use regular values. Quote from MSDN :

Each optional parameter has a default value as part of its definition. If no argument is passed for this parameter, the default value is used. The default value must be one of the following expression types:

  • constant expression;

  • expression of the form new ValType (), where ValType is a value of type, for example, an enumeration or structure;

  • default form expression (ValType), where ValType is a value of type.


My understanding (to this day) was that something public could be used if all the other "link" elements are also publicly available.

Well, technically speaking, that's right. But in your scenario, both members are available because they are defined in the same class, but if const in our case is defined outside the Program class, it will not be available inside the Program class.

+9
source

When you use const, the compiler replaces all occurrences of the variable with the actual value, so your code will be the same:

 public static void DoSomething(int value = 2) 
+5
source

Constant variables are replaced at compile time, so they never exist in the produced assembly. Thus, code that uses a constant is really identical to directly using the constant value directly. The advantage is that you can reuse a constant in another place and only need to change it in one place.

Now, since constants are always replaced at compile time, the effect of their public or private is also quite simple: it just affects the fact that another type can access it at compile time. Therefore, using a private constant, for example, can be useful if you just want to keep this constant in the current type, while a public constant can be used throughout the application.

+5
source

All Articles