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

前端 未结 3 944
时光说笑
时光说笑 2020-12-11 18:49

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         


        
3条回答
  •  悲哀的现实
    2020-12-11 19:33

    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.

提交回复
热议问题