为什么这个 属性 会导致 StackOverFlow 异常?

Why does this property cause a StackOverFlow exception?

我稍微研究了一下这个错误,当 setter 不断被调用时,我似乎正在递归,但我真的不知道我做错了什么。我知道这可能也很简单。

 namespace PracticeCSharp
 {
class Program
{


    static void Main(string[] args)
    {
        Player test = new Player();
        test.Score = 5;
        Console.ReadLine();
    }

}
class Player
{
    public int Score
    {
        get
        {
            return Score;
        }
        set
        {
            Score = value;
        }
    }
}

}

感谢您的帮助。

因为分数 属性 是递归的。

您是要改用这个吗?

namespace PracticeCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Player test = new Player();
            test.Score = 5;
            Console.ReadLine();
        }

    }
    class Player
    {
        private int score;
        public int Score
        {
            get
            {
                return score;
            }
            set
            {
                score = value;
            }
        }
    }
}

更新:

或者您可以这样做:

namespace PracticeCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Player test = new Player();
            test.Score = 5;
            Console.ReadLine();
        }

    }
    class Player
    {
        public int Score { get; set; }
    }
}

您必须声明分数变量

 namespace PracticeCSharp
 {    
    class Program
    {


        static void Main(string[] args)
        {
            Player test = new Player();
            test.Score = 5;
            Console.ReadLine();
        }

    }
    class Player
    {
        private int _score;
        public int Score
        {
            get
            {
                return _score;
            }
            set
            {
                _score = value;
            }
        }
    }
}