ambiguity between variables in C#

后端 未结 4 1794
无人及你
无人及你 2021-02-13 20:12

I want to start by saying I did search first, and found a lot of similar issues on various other things, but not this problem exactly.

I have this code:

         


        
4条回答
  •  感动是毒
    2021-02-13 20:48

    You need to rename your private gameOver variable. Change this:

    bool gameOver = false;
    public bool GameOver {
            get { return gameOver; }
            set { gameOver = value; }
        }
    

    to

    bool _gameOver = false;
    public bool GameOver {
            get { return _gameOver; }
            set { _gameOver = value; }
        }
    

    You can't use the same variable name in a single class.

    Alternatively, assuming you're using a recent version of .Net, you could remove your private variable and just have:

    public bool GameOver { get; set; }
    

    Good luck.

提交回复
热议问题