Invalid token '=' in class, struct, or interface member declaration c#

本小妞迷上赌 提交于 2019-11-27 07:56:26

问题


this may be a simple question for people, but I can't see why this is occurring. here is my code 1st:

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

    }
}

now Visual studio is giving the Invalid token error on the assignment sign (=) (as per the title), and I can not see why. can anyone shed light on this please?

What I'm trying to do is set the int of Health to 100, and each time a character takes damage, then Health is decreased. Thanks all.

I'm using Visual Studio 2013 v12.0.40629.00 update 5


回答1:


Settting a default-value for auto-implemented properties is only available from C#-version 6 and upwards. Before Version 6 you have to use the constructor and set the default-value there:

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

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

To enable the compiler for VS2013 you may use this approach.




回答2:


It looks like this error happens due to version your MSBuild, old version of MSBuild can only compile C# version 4, while your code written in C# version 6 format (set default value for properties).

Example of code writing in C# version 6:

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

For MSBuild to compile your code, you need to write in C# 4 style

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

Or

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

Hope it helps




回答3:


the answer was:

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

    }
}

making the constructor a function with () and not as PLayerCharacter{ etc.

thanks to all, back into my hole I go.



来源:https://stackoverflow.com/questions/36179921/invalid-token-in-class-struct-or-interface-member-declaration-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!