2

我对错误进行了一些研究,当设置器不断被调用时,我似乎在递归,但我真的不知道我做错了什么。我知道这可能也很简单。

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

}

谢谢你的帮助。

4

2 回答 2

2

因为 Score 属性在自身上递归。

你的意思是这样做吗?

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; }
    }
}
于 2015-07-29T22:50:03.107 回答
1

你必须声明 score 变量

 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;
            }
        }
    }
}
于 2015-07-29T22:57:02.243 回答