简体   繁体   English

将私有常量用作公共方法的默认参数背后的想法是什么?

[英]What's the idea behind allowing private constant to be used 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 public whereas the default value "redirects" to a constant private variable. 该方法是公共的,而默认值“重定向”到常量私有变量。

My question: 我的问题:

What is/was the idea behind this "concept"? 这个“概念”背后的想法是什么?

My understanding (until today) was, that something public can only be used if all other "referenced" elements are also public. 我的理解(直到今天)是,只有所有其他“引用”元素也是公开的,才能使用公共事物。

Update: 更新:

I just ILSpy-decompiled my class to find: 我只是ILSpy反编译我的课找到:

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 the private constant as a default parameter is being done in eg a library, the user of the library still sees the default parameter. 因此,如果私有常量作为默认参数在例如库中完成,则库的用户仍然可以看到默认参数。

What is/was the idea behind this "concept"? 这个“概念”背后的想法是什么?

The idea is that as const value is static and never changes - you can use it as default value for method's optional parameters same as you can use normal values. 这个想法是因为const值是静态的并且永远不会改变 - 你可以将它用作方法的可选参数的默认值,就像使用普通值一样。 A quote from MSDN : 来自MSDN的引用:

Each optional parameter has a default value as part of its definition. 每个可选参数都有一个默认值作为其定义的一部分。 If no argument is sent for that parameter, the default value is used. 如果没有为该参数发送参数,则使用默认值。 A default value must be one of the following types of expressions: 默认值必须是以下类型的表达式之一:

  • a constant expression; 一个恒定的表达;

  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct; 表达式为新的ValType(),其中ValType是值类型,例如枚举或结构;

  • an expression of the form default(ValType), where ValType is a value type. 形式为default(ValType)的表达式,其中ValType是值类型。


My understanding (until today) was, that something public can only be used if all other "referenced" elements are also public 我的理解(直到今天)是,只有所有其他“引用”元素也是公开的,才能使用公共事物

Well technically speaking it's correct. 从技术上讲,这是正确的。 But in your scenario both members are accessible as they are defined in the same class however should const in our case be defined outside of class Program it'd be inaccessible inside class Program . 但是在你的场景中,两个成员都是可访问的,因为它们是在同一个类中定义的,但是在我们的例子中, const应该在类之外定义Program它在类Program是不可访问的。

当您使用const时,编译器会将所有出现的变量替换为实际值,因此您的代码与此相同:

public static void DoSomething(int value = 2)

Constant variables are replaced at compile-time, so they never really exist in the produced assembly. 常量变量在编译时被替换,因此它们从未真正存在于生成的程序集中。 So code using a constant is really identical to just using the constant value directly. 所以使用常量的代码与直接使用常量值完全相同。 The benefit is just that you can reuse the constant elsewhere and only need to change it in one location. 好处是您可以在其他地方重用常量,只需要在一个位置更改它。

Now, since constants are always replaced at compile-time, the effect of making them public or private is also quite simple: It just affects what other type can access it at compile-time. 现在,由于常量在编译时总是被替换,因此将它们设置为public或private的效果也非常简单:它只影响其他类型在编译时可以访问它的内容。 So using a private constant for example can be helpful if you just want to keep that constant to the current type, whereas a public constant can be used across the whole application. 因此,如果您只想将常量保持为当前类型,则使用私有常量可能会有所帮助,而公共常量可以在整个应用程序中使用。

The name DefaultValue is private, but the number 2 is still the number 2 . 名称DefaultValue是私有的,但数字2仍然是数字2

Because DefaultValue is private, we cannot access Program.DefaultValue from outside of Program . 由于DefaultValue是私有的,因此我们无法从Program外部访问Program.DefaultValue Presumably we wouldn't particularly want to. 大概我们不会特别想要。

And because we've bothered to define DefaultValue at all, it's presumably something that we do care about when we are working on how Program works. 而且因为我们根本无法定义DefaultValue ,所以在我们研究Program工作原理时,我们可能会关心它。

So when we come to define a default value for DoSomething there's presumably some logical reason why the value we want there happens to be the same as the value DefaultValue . 因此,当我们为DoSomething定义默认值时,可能有一些逻辑上的原因,为什么我们想要的值恰好与值DefaultValue相同。

And as such, it's presumably beneficial to be able to use that constant there, for much the same reasons as we would find constants beneficial anywhere. 因此,能够在那里使用那个常数可能是有益的,原因与我们在任何地方都能找到有益的常数一样。

And since DefaultValue is just a Program -specific way of saying 2 , there's no real reason why we can't. 而且由于DefaultValue只是一种特定于Program的说法2 ,所以我们没有真正的原因。

Of course, the metadata would reflect this as 2 rather than the (meaningless to the outside) DefaultValue , but then that would hold if the const was public anyway (the metadata about default values gives only the value, not whether or not it related to any defined constants). 当然,元数据会将其反映为2而不是(对于外部无意义) DefaultValue ,但是如果const无论如何都是public的(关于默认值的元数据仅给出值,而不是它是否与任何已定义的常量)。

So there's no downside. 所以没有缺点。

So considering that: 所以考虑到:

  1. There's an advantage to the implementer. 实施者有一个优势。
  2. There's no disadvantage to the user, over just a use of a literal 2 . 用户没有任何缺点,只使用文字2
  3. Preventing it would have to introduce a special rule to be an exception to the rule that defined constants can be used anywhere a constant value from a literal can. 防止它必须引入一个特殊规则作为规则的例外,定义的常量可以在文字的常量值的任何地方使用。

Why not? 为什么不?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM