来自 get 和 set 的 StackOverflow 异常 [英] StackOverflow Exception from get and set

查看:15
本文介绍了来自 get 和 set 的 StackOverflow 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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);
            }
        }
    }
}

但我收到堆栈溢出异常.

But I get a stack overflow exception.

我知道这是因为我没有正确执行 get 和 set 操作并且进入了无限循环,但是我不明白为什么会发生这种情况?

I know it is because I am not doing my get and sets correctly and am entering an infinite loop, but I cannot see why exactly this is happening?

推荐答案

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

您没有使用支持字段,而是在属性设置器中设置属性本身.

you aren't using backing fields, but setting the property itself from within the property setter.

您可以通过使用 1) 自动属性来解决此问题

You can fix this by using 1) an auto property

public DateTime Time { get; set; }

或 2) 支持字段

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

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

they both equate to the same code.

解释一下,当您在代码中获得 time 时:

For an explanation, when you get time in your code:

get { return this.time; } 

它必须检索time的值才能返回.它通过在 time 上调用 get 来实现,它必须检索 time 等的值.

it has to retrieve the value of time to return. It does that by calling the get on time, which has to get retrieve the value of time, etc.

这篇关于来自 get 和 set 的 StackOverflow 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆