c#中使用自动属性减少代码输入量
- 作者: BIG笑工坊929
- 来源: 51数据库
- 2021-11-02
public class product
{
private string name;
public string name
{
get
{
return name;
}
private set
{
name = value;
}
}
private decimal price;
public decimal price
{
get
{
return price;
}
set
{
price = value;
}
}
public product(string name, decimal price)
{
this.price = price;
this.name = name;
}
}
可以改写为:
public class product
{
public string name
{
get;
private set;
}
public decimal price
{
get;
set;
}
public product(string name, decimal price)
{
name = name;
price = price;
}
public override string tostring()
{
return string.format("{0}:{1}", this.name, this.price);
}
}
代码是不是简化了很多!
注意:
不能定义只读或者只写的属性,必须同时提供
如果想在属性中增加判断、验证等逻辑,则只能用传统的属性定义方法实现
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源