C# get和set中的StackOverflow异常

C# get和set中的StackOverflow异常,c#,get,set,C#,Get,Set,我有以下代码: namespace QuantStrats { class Program { static void Main(string[] args) { string FilePath = "C:\\Users\\files\\DJ.csv"; StreamReader streamReader = new StreamReader(FilePath); string

我有以下代码:

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);
            }
        }
    }
}
namespace-QuantStrats
{
班级计划
{
静态void Main(字符串[]参数)
{
string FilePath=“C:\\Users\\files\\DJ.csv”;
StreamReader StreamReader=新的StreamReader(文件路径);
弦线;
列表数据=新列表();
而((line=streamReader.ReadLine())!=null)
{
数据勾号=新数据();
string[]value=line.Split(',');
勾选设置字段(值[1],值[2]);
数据。添加(勾选);
}
对于(int ii=0;ii
但我得到一个堆栈溢出异常

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

public DateTime time
{
    get { return this.time; }
    set
    {
        this.time = value;                
    }
}
您没有使用支持字段,而是从属性设置器中设置属性本身

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

public DateTime Time { get; set; }
或2)支持字段

private DateTime _time;
public Datetime Time 
{
    get { return _time; }
    set { _time = value; }
} 
它们都等同于相同的代码

作为解释,当您在代码中获得时间时:

get { return this.time; } 
它必须检索
time
的值才能返回。它通过调用
time
上的
get
来实现这一点,它必须获取并检索
time
的值,等等

我不明白为什么会发生这种事

当您“get”
price
时,调用
price
的getter,它调用
price
的getter,它调用
price
的getter,它

如果不想弄乱备份字段,只需使用“自动实现属性”:

    public DateTime Time {get; set;}
    public double Price {get; set;}
其他一些意见:

  • 属性名称的标准约定是以大写字母开头,这就是为什么我在示例中将属性更改为
    Time
    Price

  • 如果你做浮点运算,你可能想考虑使用<代码>十进制< /代码>一个属性,比如“代码>价格>代码”,因为<代码>双< /代码>在表示1.1的十进制数时有一些微小的不精确性。code>decimal
    将在不损失任何精度的情况下精确存储数字

  • 仅在
    catch
    块中写入控制台似乎不正确。您基本上忽略了错误(从逻辑流的角度)。我不会在类中接受字符串并对其进行解析,而是在调用代码中进行验证,并在将输入传递给类之前确保输入有效


  • 属性getter和setter实际上只是
    getXXX
    setXXX
    方法(它们就是这样编译的)。因为您是从属性本身设置属性的,所以如果您在一个方法上无休止地重复出现,就会发生这种情况

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

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

    支持字段,而不是支持属性。是的,感谢@JonSkeet,尝试成为第一位,并且键入得太快。除非您在设置/获取属性时需要进行附加验证或其他逻辑,否则肯定会选择#1。
    public DateTime time()
    {
        return time();
    }