Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无效令牌'=';c#程序中的类内结构接口 使用System.Text; 使用System.Threading.Tasks; 名称空间程序 { 公共课堂用书 { 公共字符串标题; 公共字符串作者; 公共int页面; 书页=10页; } }_C# - Fatal编程技术网

无效令牌'=';c#程序中的类内结构接口 使用System.Text; 使用System.Threading.Tasks; 名称空间程序 { 公共课堂用书 { 公共字符串标题; 公共字符串作者; 公共int页面; 书页=10页; } }

无效令牌'=';c#程序中的类内结构接口 使用System.Text; 使用System.Threading.Tasks; 名称空间程序 { 公共课堂用书 { 公共字符串标题; 公共字符串作者; 公共int页面; 书页=10页; } },c#,C#,我不确定为什么会出现无效令牌错误。请提供帮助。如果要设置页面的默认值,可以使用此代码 public class Book { public string title { get; set; } public string author { get; set; } public int pages { get; set; } = 10; } 但如果要在创建类后设置值,可以使用以下代码: 第一次应该创建对象的实例并设置值 public class Book { pu

我不确定为什么会出现无效令牌错误。请提供帮助。

如果要设置页面的默认值,可以使用此代码

public class Book
{
    public string title { get; set; }
    public string author { get; set; }
    public int pages { get; set; } = 10;
}
但如果要在创建类后设置值,可以使用以下代码: 第一次应该创建对象的实例并设置值

public class Book
{
    public string title { get; set; }
    public string author { get; set; }
    public int pages { get; set; }
}
Book book = new Book { pages = 10 };
您还可以使用
构造函数
设置默认值

public class Book
{
    public Book()
    {
        this.pages = 10;
    }
    public string title { get; set; }
    public string author { get; set; }
    public int pages { get; set; } 
}

这不是正确的语法。如果要将页面初始化为10,则应执行
public int pages=10
或创建一个构造函数并编写
页面=10在构造函数中。这是最后一行。不能仅在类声明中执行代码。它需要在构造函数或其他方法中。或者如果您想要一个默认值,只需写
public int pages=10哦,我明白了,好的,谢谢!