gpt4 book ai didi

c# - 在类构造函数中设置默认值 C#

转载 作者:太空狗 更新时间:2023-10-29 22:11:16 26 4
gpt4 key购买 nike

我需要一个默认值集和许多不同的页面访问和更新..最初我可以像这样在类构造函数中设置默认值吗?在 C# .NET 中执行此操作的正确方法是什么?

public class ProfitVals
{

private static double _hiprofit;

public static Double HiProfit
{
get { return _hiprofit; }

set { _hiprofit = value; }
}

// assign default value

HiProfit = 0.09;

}

最佳答案

你可以把它放在声明中:private static double _hiprofit = 0.09;或者,如果它是更复杂的初始化,您可以在静态构造函数中进行:

   private static double _hiprofit; 
static ProfitVals()
{
_hiprofit = 0.09;
}

前者是首选,因为后者会付出性能损失:http://blogs.msdn.com/b/brada/archive/2004/04/17/115300.aspx

关于c# - 在类构造函数中设置默认值 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5251621/

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