无效令牌'=';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代码>哦,我明白了,好的,谢谢!