Ara*_*ash 1 .net c# get properties
朋友我在c#中使用get或set时遇到问题当我使用get或set in给出错误时(无效令牌{在课堂上)请参阅下面的代码,我有这个问题在里面
static int abcd
{
get
{
return _abcd;
}
}
Run Code Online (Sandbox Code Playgroud)
感谢名单
这是完整的代码,我没有任何代码的问题,但只是这样:
namespace ConsoleApplication2
{
class Program
{
class Car
{
private int _speed;
public int Speed;
{
get
{
return _speed
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您发布的片断是好的,因为它是,也是在关于错误,因为它有正确数量{来},并在正确的顺序.
查看放置它的位置(可能在类之外),或者}在文件中查找额外的内容.
更新:(以下编辑内容)
你的问题在这里:
public int Speed; // <-- ; should not be here
Run Code Online (Sandbox Code Playgroud)
和:
return _speed // <-- missing the ;
Run Code Online (Sandbox Code Playgroud)
该属性应该像这样实现:
public int Speed
{
get
{
return _speed;
}
}
Run Code Online (Sandbox Code Playgroud)
您的代码中有两个错误.
试试这个:
namespace ConsoleApplication2
{
class Program
{
class Car
{
private int _speed;
public int Speed // <-- no semicolon here.
{
get
{
return _speed; // <-- here
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到你最初发布的代码格式很糟糕.我建议您在Visual Studio中自动格式化文档以使大括号排成一行.这应该使错误更加明显.当代码格式错误时,您知道附近有错误.您可以在菜单中找到此选项:编辑 - >高级 - >格式文档或使用键盘快捷键(Ctrl-E D对我而言,但根据您的设置,可能会有所不同).
我还建议你考虑使用自动实现的属性,而不是完全写出getter:
namespace ConsoleApplication2
{
class Program
{
class Car
{
public int Speed { get; private set; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
970 次 |
| 最近记录: |