【发布时间】:2013-06-12 09:44:43
【问题描述】:
我有这个属性
/// <summary>
/// The production date of the device is contained in its serial number.
/// </summary>
public DateTime Date
{
get
{
return SerialNumber.Date;
}
}
但是,我希望我可以这样写
/// <summary>
/// The production date of the device is contained in its serial number.
/// </summary>
public DateTime Date
{
get return SerialNumber.Date;
}
但我收到错误{ or ; expected。
现在我的问题是:为什么我需要大括号,而我不想标记范围的开始和结束,因为只有一行代码。
我确实需要使用大括号并不重要。但在我看来,它与 C# 代码的其他解释不一致。
你可以使用
if(DoNotUseBraces) return null; 例如,不需要大括号。
甚至
if (SomeBoolean)
return null;
同样,不需要大括号。我猜它与:Why do methods with only one statement need braces? 相同,但 get 没有像 get() 那样实现 (),例如
【问题讨论】:
-
类和方法也需要大括号,
try/catch/finally和其他一些结构也是如此。这些事情背后通常没有什么理由(并且正确地遵循编码约定无论如何都会使这些特质成为一个有争议的问题)。
标签: c# properties get set curly-braces