gpt4 book ai didi

c# - 我可以检索方法中参数的默认值吗?

转载 作者:行者123 更新时间:2023-12-02 14:44:34 25 4
gpt4 key购买 nike

示例:

public static double ComputeFoo(double nom, double den, double epsilon = 2.2e-16)
{
double den1 = den == 0.0 ? epsilon : den;
// den1 can still be zero if epsilon is zero
// is there any way to retrieve 2.2e-16 here and assign it to den1?
return nom/den1;
}

有没有办法检索 2.2e-16 值并在方法中使用它?

P.S.:我知道对于这个特定的示例,我可以调用 ComputeFoo(nom, den1)

最佳答案

您可以在类中的某个位置设置一个常量值,并将其作为默认值传递给该方法。到达那里后,您可以检查传递的值是否与常量不同,反之亦然:

static void Main(string[] args)
{
Test(0);
}

const int constantValue = 15;

static int Test(int testValue = constantValue)
{
Console.WriteLine(testValue);
Console.WriteLine(constantValue);

return constantValue;
}

注意:constantValue 必须是常量才能构建成功。

关于c# - 我可以检索方法中参数的默认值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60552130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com