嘿,我是C#的新手,我正在学习一个教程,但是我的代码中出现了错误......它在“get; set;”中显示“Identifier expected”,并且还提供错误“无效的令牌” ';'在类,结构或接口成员声明中“at set。
非常感谢任何帮助。
谢谢!
public class Invoice
{
public int ID ( get; set; )
public string Description ( get; set; )
public decimal Amount ( get; set; )
}
[TestClass]
public class ReferenceTypesAndValueTypes
{
[TestMethod]
public void IdentityTest()
{
Invoice firstInvoice = new Invoice();
firstInvoice.ID = 1;
firstInvoice.Description = "Test";
firstInvoice.Amount = 0.0M;
Invoice secondInvoice = new Invoice();
secondInvoice.ID = 1;
secondInvoice.Description = "Test";
secondInvoice.Amount = 0.0M;
Assert.IsFalse(object.ReferenceEquals(secondInvoice, firstInvoice));
Assert.IsTrue(firstInvoice.ID == 1);
secondInvoice.ID = 2;
Assert.IsTrue(secondInvoice.ID == 2);
Assert.IsTrue(firstInvoice.ID == 1);
secondInvoice = firstInvoice;
Assert.IsTrue(object.ReferenceEquals(secondInvoice,firstInvoice));
secondInvoice.ID = 5;
Assert.IsTrue(firstInvoice.ID == 5);
}
}
答案 0 :(得分:6)
您需要用括号()
{}
E.g。
public class Invoice
{
public int ID { get; set; }
public string Description { get; set; }
public decimal Amount { get; set; }
}
答案 1 :(得分:1)
在自动属性中,语法糖允许你不使用getter / setter的实现 - 所以你应该这样:
SomeProperty { get; set; }
它等同于:
SomeProperty
{
get { return _somePropertyBackingField; }
set { _somePropertyBackingField = value; }
}
这些都不需要括号,因为这是结构符号 - 括号通常保留用于转换和方法调用
SomeProperty ( get; set; )
对翻译来说意义不大
您知道Visual Studio中有代码段可以为您执行此操作:
只需输入" prop"并点击两次标签。试试吧:) - 没有更多的拼写错误