类,结构或接口成员声明中的标记’=’无效c#

类,结构或接口成员声明中的标记’=’无效c#,第1张

概述对于人们来说,这可能是一个简单的问题,但我不明白为什么会这样.这是我的代码第1条: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace GameCore{ public class Pla 对于人们来说,这可能是一个简单的问题,但我不明白为什么会这样.这是我的代码第1条:
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在赋值符号(=)上给出了无效的令牌错误(根据标题),我看不出原因.有谁可以解释这个吗?

我要做的是将生命的int设置为100,每次角色受到伤害时,生命值会降低.谢谢大家.

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

解决方法 为自动实现的属性设置默认值仅适用于C#-version 6及更高版本.在版本6之前,您必须使用构造函数并在其中设置default-value:
public class PlayerCharacter {    public int Health { get; private set; }    public PlayerCharacter()    {        this.Health = 100;    }}

要为VS2013启用编译器,可以使用this approach.

总结

以上是内存溢出为你收集整理的类,结构或接口成员声明中的标记’=’无效c#全部内容,希望文章能够帮你解决类,结构或接口成员声明中的标记’=’无效c#所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1251004.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存