获得stackoverflowexception并且不知道原因。有人可以帮忙吗?

时间:2014-09-12 13:49:11

标签: c# stack-overflow

我是新手,我正在逐步遵循说明,但由于某种原因继续得到stackoverflowexception。我究竟做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;

namespace assignment3
{
    class changeValue
    {
        //Create a class called changeValue that declares 2 integer class variables:  value1 and 
        //  value2.  These should be declared as public and you should not use automatic properties 
        // to declare them. 
        public int value1
        {
            get
            {
                return value1;
            }//end get
            set
            {
                value1 = value;
            }//end set
        }
        public int value2
        {
            get
            {
                return value2;
            }//end get
            set
            {
                value2 = value;
            }//end set
        }

    public changeValue(int val1, int val2)
    {
        //here is the constructor where you code the if statements
        int value1 = val1;
        int value2 = val2;

        if (value1 > 5)
        { 
            value1 = val1; 
        }
        if (val1 <= 5)
        {
            value1 = (val1+val2);
        }
        if (val2 < 10)
        {
            value2 = (val2 * val2 + 5);
        }
        if (val2 >= 10)
        {
            value2 = val2;
        }
    }

    public void printit()
    {
        //here is the printit method used to print the results
       Console.WriteLine("The calculated value is:" + (value1 * value2));

    }
}
class assignment3
{
    public static void Main(string[] args)
    {
        //declare the local val1 and val2 integer variables
        int val1;
        int val2;    

        //prompt the user for input of two integers
        //don’t forget to convert from the string input to integer
        Console.Write("Enter an integer value: "); //obtain user input
        val1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter a second integer value: "); //obtain user input
        val2 = Convert.ToInt32(Console.ReadLine());

        //instantiate a changeValue object here
        changeValue myValue = new changeValue(val1,val2);

        myValue.printit();//call the object method printit here
    }
}
}

4 个答案:

答案 0 :(得分:4)

这些是你的问题,它们是自我引用的。

    public int value1
    {
        get
        {
            return value1;
        }//end get
        set
        {
            value1 = value;
        }//end set
    }
    public int value2
    {
        get
        {
            return value2;
        }//end get
        set
        {
            value2 = value;
        }//end set
    }

将它们更改为:

    public int value1 { get; set; }
    public int value2 { get; set; }

答案 1 :(得分:4)

value1的getter正在调用自身,它会无限地递归,直到调用堆栈已满。

不使用自动属性的方法(根据代码注释分配的方法)是使用单独的支持字段:

    private int _value1;

    public int value1
    {
        get
        {
            return _value1;
        }//end get
        set
        {
            _value1 = value;
        }//end set
    }

答案 2 :(得分:1)

您创建的本质上是循环引用。你的getter和setter需要backer字段。

private int _value1;
private int _value2;

public int Value1
    {
        get
        {
            return _value1;
        }//end get
        set
        {
            _value1= value;
        }//end set
    }
    public int Value2
    {
        get
        {
            return _value2;
        }//end get
        set
        {
            _value2 = value;
        }//end set
    }

你拥有它的方式,通过设置value1 = value,你创建了一个无限循环。

答案 3 :(得分:1)

changeValue中的商家信息会产生StackOverflowException

public int value1
{
    get
    {
        return value1;  // --> this line is referencing the property itself and will StackOverflow!
    }//end get
    set
    {
        value1 = value;
    }//end set
}

相反,请尝试使用简单的自动属性:

public int value1 { get; set; }

或者使用具有不同名称的后备商店(请注意下划线):

private int _value1;
public int value1
{
    get
    {
        return _value1;
    }//end get
    set
    {
        _value1 = value;
    }//end set
}