我有以下代码。我们争论哪种方法更适合内存性能。如果我们不使用单独的变量并使用它,它会有什么不同吗?它会影响那么这是可能的解决方案吗?
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
chkTreatSR.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = value.Split(',').Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
OR
/// <summary>
/// Treatment Point Types
/// </summary>
public string DWTreatment
{
get
{
StringBuilder sbStatus = new StringBuilder();
.........
}
set
{
var values = value.Split(',');
chkTreatSR.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.SR).ToString());
chkTreatIT.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.IT).ToString());
chkTreatEP.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.EP).ToString());
chkTreatWW.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.WW).ToString());
chkTreatQC.Checked = values.Contains(((int)AgencyListReportTemplate.SamplingPointTypesEnum.QC).ToString());
isSelectionSetByPage = true;
}
}
感谢。
答案 0 :(得分:0)
第二个示例将减少内存分配,因为每次调用string.Split(,)都会分配一个新的字符串数组。
如果这是代码中的“热点”,您可能会考虑根本不调用string.Split,而是使用Regex搜索您的枚举。