来自 get 和 set 的 StackOverflow 异常

StackOverflow Exception from get and set

我有以下代码:

namespace QuantStrats
{
    class Program
    {
        static void Main(string[] args)
        {
            string FilePath = "C:\Users\files\DJ.csv";
            StreamReader streamReader = new StreamReader(FilePath);
            string line;
            List<Data> Data = new List<Data>();     

            while ((line = streamReader.ReadLine()) != null)
            {
                Data Tick = new Data();
                string [] values = line.Split(',');
                Tick.SetFields(values[1], values[2]);
                Data.Add(Tick);
            }

            for (int ii = 0; ii < Data.Count; ii++)
            {
                Data TickDataValues = new Data();
                TickDataValues = Data[ii];             
                Console.Write("TIME :" + TickDataValues.time + " Price : " + TickDataValues.price +  Environment.NewLine);
            }

            Console.ReadLine();
        }
    }

    class Data
    {
        public DateTime time
        {
            get { return this.time; }
            set
            {
                this.time = value;                
            }
        }

        public double price
        {
            get { return this.price; }
            set
            {
                this.price = value;                
            }
        }

        public void SetFields(string dateTimeValue, string PriceValue)
        {
            try
            {
                this.time = Convert.ToDateTime(dateTimeValue);
            }
            catch
            {
                Console.WriteLine("DateTimeFailed " + dateTimeValue + Environment.NewLine);
            }

            try
            {
                this.price = Convert.ToDouble(PriceValue);
            }
            catch
            {
                Console.WriteLine("PriceFailed " + PriceValue + Environment.NewLine);
            }
        }
    }
}

但是我得到一个堆栈溢出异常。

我知道这是因为我没有正确执行 get 和 set 并进入无限循环,但我不明白为什么会这样?

public DateTime time
{
    get { return this.time; }
    set
    {
        this.time = value;                
    }
}

您没有使用支持字段,而是从 属性 setter.

中设置 属性 本身

您可以使用 1) 自动 属性

来解决这个问题
public DateTime Time { get; set; }

或 2) 支持字段

private DateTime _time;
public Datetime Time 
{
    get { return _time; }
    set { _time = value; }
} 

它们都等同于相同的代码。

为了解释,当您在代码中得到 time 时:

get { return this.time; } 

它必须检索 time 的值到 return。它通过在 time 上调用 get 来实现,它必须检索 time 的值,等等

I cannot see why exactly this is happening?

    public double price
    {
        get { return this.price; }
        set
        {
            this.price = value;                
        }
    }

当你 "get" price 时,getter for price 被调用,它调用 getter for price,它调用price 的 getter,其中...

如果您不想弄乱支持字段,只需使用自动实现属性:

    public DateTime Time {get; set;}
    public double Price {get; set;}

其他一些观察:

  1. 属性 名称的标准约定是以大写字母开头,这就是为什么我将您的属性更改为 TimePrice示例。

  2. 如果您进行任何浮点运算,您可能需要考虑使用 decimal 作为 属性,例如 Price,因为 double 有在表示像 1.1 这样的十进制数字时有些不精确。 decimal 将在不损失任何精度的情况下精确存储数字。

  3. 仅在 catch 块中写入控制台似乎不正确。您基本上忽略了错误(从逻辑流程的角度来看)。我不会在 class 中接受字符串并解析它们,而是在 调用 代码中进行验证并确保输入有效,然后再将它们传递给 class.

属性 getter 和 setter 实际上只是 getXXXsetXXX 方法(这就是它们的编译方式)。因为你从 属性 本身设置了 属性,所以如果你在一个方法上无休止地重复。

public DateTime time()
{
    return time();
}

如其他答案所述,您可以使用支持字段或自动实现的属性。