gpt4 book ai didi

c# - 如何为数组数据成员定义 get 和 set?

转载 作者:可可西里 更新时间:2023-11-01 03:12:20 26 4
gpt4 key购买 nike

我正在创建一个类 Customer,它具有以下数据成员和属性:

private string customerName;
private double[] totalPurchasesLastThreeDays; //array of 3 elements that will hold the totals of how much the customer purchased for the past three days i.e. element[0] = 100, element[1] = 50, element[2] = 250

public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}

public double[] TotalPurchasesLastThreeDays
{
?
}

如何定义数组数据成员的get和set?

最佳答案

你想要一个indexer吗? ?

public double this[int i] {
get { return totalPurchasesLastThreeDays[i]; }
set { totalPurchasesLastThreeDays[i] = value; }
}

因为否则这个问题听起来有点奇怪,因为您已经在代码中实现了一个属性并且显然有能力这样做。

关于c# - 如何为数组数据成员定义 get 和 set?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9751871/

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