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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9751871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:42:32  来源:igfitidea点击:

How do define get and set for an array data member?

c#

提问by Bob

I am making a class Customerthat has the following data members and properties:

我正在创建一个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
{
?
}

How do I define the get and set for the array data member?

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

采纳答案by Jason Evans

You can use an auto-property:

您可以使用自动属性:

public class Customer
{
    public string CustomerName { get; set; }

    public double[] TotalPurchasesLastThreeDays { get; set; }
}

Or if you want:

或者如果你想要:

public class Customer
    {
        private double[] totalPurchasesLastThreeDays; 

        public string CustomerName { get; set; }

        public double[] TotalPurchasesLastThreeDays
        {
            get
            {
                return totalPurchasesLastThreeDays;
            }
            set
            {
                totalPurchasesLastThreeDays = value;
            }
        }
    }

And then in the constructor, you can set some default values:

然后在构造函数中,您可以设置一些默认值:

public Customer()
{
    totalPurchasesLastThreeDays = new double[] { 100, 50, 250 };
}

回答by ionden

public double[] TotalPurchasesLastThreeDays
{
get
{
    return totalPurchasesLastThreeDays;
}
set
{
    totalPurchasesLastThreeDays = value;
}
}

回答by BrokenGlass

If you want the array to be settable from the outside anyway, the most convenient is to use an auto property and just remove the private field you already have:

如果您希望数组可以从外部设置,最方便的是使用 auto 属性并删除您已经拥有的私有字段:

public double[] TotalPurchasesLastThreeDays { get; set; }

回答by Royi Namir

public double[] TotalPurchasesLastThreeDays
{
 get

  {
    return totalPurchasesLastThreeDays;
  }

set

{
 totalPurchasesLastThreeDays=value;
}
}

回答by Joey

Do you want an indexer?

你想要一个索引器吗?

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

Because otherwise the question sounds a little weird, given that you already implemented a property in your code and are obviously capable of doing so.

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

回答by Ry-

One way would be this, (I'm assuming the point of this is so that the array can no longer be modified after setting):

一种方法是这样,(我假设这样做的目的是为了在设置后无法再修改数组):

public double[] TotalPurchasesLastThreeDays
{
    get {
        return totalPurchasesLastThreeDays;
    }
    set {
        totalPurchasesLastThreeDays = (double[])value.Clone();
    }
}

But... do you really want to do that? It may be more convenient and intuitive to just do the same thing as you would usually.

但是……你真的要那样做吗?像往常一样做同样的事情可能会更方便和直观。

回答by dee-see

You might be asking yourself this question because you think that assigning to your array is different than a regular variable?

您可能会问自己这个问题,因为您认为分配给您的数组与常规变量不同?

In that case, you have to realize that when you call TotalPurchasesLastThreeDays[3] = 14.0you are actually using the getter and not the setter. The setter is used to change the array itself and not the values it contains. So coding the getter and setter isn't any different with an array than with any other variable type.

在这种情况下,您必须意识到,当您调用时,TotalPurchasesLastThreeDays[3] = 14.0您实际上是在使用 getter 而不是 setter。setter 用于更改数组本身而不是它包含的值。因此,使用数组对 getter 和 setter 进行编码与使用任何其他变量类型没有任何不同。

回答by digEmAll

If you want to allow this:

如果你想允许这个:

myCustomer.TotalPurchasesLastThreeDays[2] = 3.1415;
var foo = myCustomer.TotalPurchasesLastThreeDays[1];

but not this:

但不是这个:

myCustomer.TotalPurchasesLastThreeDays = new Customer[]{ ... };

Define only the getter i.e. :

仅定义吸气剂,即:

public double[] TotalPurchasesLastThreeDays 
{
   get { return this.totalPurchasesLastThreeDays; }
}

Instead, if you need to change the array instance from outside the class, simply follow the other answers.

相反,如果您需要从类外部更改数组实例,只需按照其他答案即可。

回答by nidhin

public int this [int varialbeindex]
{
    get
    {
        return totalpurchaseslastdays[index];
    }
    set
    {
        totalpurchaseslastdays[index]=value;
    }
}