Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在获取stackoverflowexception,但不知道为什么。有人能帮忙吗?_C#_Stack Overflow - Fatal编程技术网

C# 正在获取stackoverflowexception,但不知道为什么。有人能帮忙吗?

C# 正在获取stackoverflowexception,但不知道为什么。有人能帮忙吗?,c#,stack-overflow,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

我是新手,我正在一步一步地遵循说明,但出于某种原因,我一直在获取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
    }
}
}

这些是你的问题,它们是自我参照

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

您创建的基本上是循环引用。你的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,您已经创建了一个无限循环。

value1的getter正在调用它自己,它无限递归,直到调用堆栈满为止

不使用基于代码注释指定的自动属性的方法是使用单独的支持字段:

    private int _value1;

    public int value1
    {
        get
        {
            return _value1;
        }//end get
        set
        {
            _value1 = value;
        }//end set
    }
changeValue中的属性将导致StackOverflowException:

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

public int value1 { get; set; }
或者使用与您的属性具有不同名称的备份存储请注意下划线:

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

value1的getter返回value1,它调用value1的getter,value1返回value1,它调用value1的getter,value1在不到三分钟内回答了六个问题。。。哇。10分钟内回答最多的记录是什么。@eddie_cat垒球有很多本垒打……人们对这个特殊的例外有一些倾向,所以答案来得很快好了,现在我更明白了,为什么这本该死的教科书不能更好地解释事情呢?哈哈。我想如果你有一本教科书,你会忽略某些事情。也许它使用了大写字母V和小写字母V。我不认为你发布的确切代码会出现在教科书中。如果是的话,我会找一本不同的书教我编码。谢谢大家的帮助。但是现在当它计算时,计算值总是返回0。这是一个完全不同的问题,但是你在其中输入了什么?我把它改成了你的建议。问题是这样的吗?
private int _value1;
public int value1
{
    get
    {
        return _value1;
    }//end get
    set
    {
        _value1 = value;
    }//end set
}