【问题标题】:Invalid token '=' in class, struct, or interface member declaration c#类、结构或接口成员声明中的无效标记“=”c#
【发布时间】:2016-07-10 20:20:05
【问题描述】:

这对人们来说可能是一个简单的问题,但我不明白为什么会发生这种情况。这是我的第一个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameCore
{
    public class PlayerCharacter
    {

        public void Hit(int damage)
        {
            Health -= damage;

            if (Health <= 0)
            {
                IsDead = true;
            }
        }

        public int Health { get; private set; } = 100;
        public bool IsDead{ get; private set; }

    }
}

现在 Visual Studio 在赋值符号 (=) 上给出了 Invalid token 错误(根据标题),我不明白为什么。有人能解释一下吗?

我正在尝试将生命值的 int 设置为 100,并且每次角色受到伤害时,生命值都会降低。谢谢大家。

我正在使用 Visual Studio 2013 v12.0.40629.00 更新 5

【问题讨论】:

  • @SimonKarlsson 实际上,无论如何,每次命中后可能都会执行死亡检查。就我个人而言,我只是让 Hit 立即将其作为布尔值返回,以避免再次调用。无论如何,优化他的游戏不是这里的问题。
  • 谢谢大家,我会考虑,这是一个简单的 specFlow 实验类......我将在以后更改公共布尔值......只是想让事情顺利进行。再次感谢,G。
  • @Nyerguds 你是对的,我们不应该,但是我的意思是确保正确性然后优化。想象一下他添加了一个 revive 方法的场景,那么他在这里也需要更改 bool IsDead 的值。我的意思是IsDead 更多的是检查而不是实际值。 :)
  • @GraemePhillips 你应该accept HimBromBeere 的回答;尽管他们忘记了示例代码中的括号,但答案确实明确指出您需要将初始化放在类构造函数中,这是您问题的解决方案。
  • @Nyerguds 现在已经完成了,再次感谢大家的帮助。

标签: c# get set


【解决方案1】:

看起来此错误是由于您的 MSBuild 版本造成的,旧版本的 MSBuild 只能编译 C# 版本 4,而您的代码以 C# 版本 6 格式编写(设置属性的默认值)。

使用 C# 版本 6 编写的代码示例:

 public static string HostName { get; set; } = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";

要让 MSBuild 编译您的代码,您需要使用 C# 4 风格编写

public static string HostName { get; set; }
public SomeConstructor()
        {
            HostName = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";... }

或者

 public static string HostName
        {
            get
            {
                return ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";
            }
        }

希望对你有帮助

【讨论】:

  • 这条评论对我的帮助比上述更多,因为我可以在我的计算机上构建我的 VS2017 没有问题的项目,但后来我在我们的 CI 系统 (TeamCity) 上遇到了这个构建失败,这是使用旧版本的 MSBuild 来构建项目。因此,将 C# 6.0 属性自动初始化恢复为与 MSBuild 兼容的旧 C# 语法解决了该问题 - 并指出我需要更新我们的 TeamCity 基础架构!
【解决方案2】:

为自动实现的属性设置默认值仅适用于 C#-version 6 及更高版本。在版本 6 之前,您必须使用构造函数并在那里设置默认值:

public class PlayerCharacter {
    public int Health { get; private set; }

    public PlayerCharacter()
    {
        this.Health = 100;
    }
}

要启用 VS2013 的编译器,您可以使用 this approach

【讨论】:

  • 感谢 HimBromBeere - 但是我在上面的主要问题中复制的代码中应该放在哪里?抱歉有点菜鸟,刚刚学习。放轻松,谢谢。格雷姆
  • 直接进入PlayerCharacter-class,最好在其顶部。
  • 嗨 HimBromBeere - 我认为我做错了什么......这是新代码,它根本不喜欢它,抱歉。命名空间 GameCore { public class PlayerCharacter { public int Health { get;私人套装; } public PlayerCharacter { this.Health = 100; } public void Hit(int damage) { Health -= damage; if (Health
  • 对不起,但现在完全迷路了。如果我尝试这样做,那么我会得到 7 个错误而不是 1 个错误。
  • Health 变量仅在 Hit 方法中显示智能感知。
【解决方案3】:

答案是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameCore
{
    public class PlayerCharacter 
    {

        public int Health { get; private set; }

        public PlayerCharacter()
        {
            this.Health = 100;
        }


        public void Hit(int damage)
        {
            Health -= damage;


            if (Health <= 0)
            {
                IsDead = true;
            }
        }




        public bool IsDead{ get; private set; }

    }
}

使构造函数成为一个带有 () 而不是 PLayerCharacter{ 等的函数。

谢谢大家,我回去了。

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2014-04-07
    • 2014-06-22
    • 1970-01-01
    • 2012-06-19
    相关资源
    最近更新 更多