不同类型的只读属性
Different Types of Readonly Properties
在 C# 6 中你可以有以下 属性:
public Uri MyProperty => new Uri();
或者你可以这样:
public Uri MyProperty1 { get; } = new Uri();
它们有什么区别?
每次访问 属性 时第一个 returns 一个新的 Uri
对象 - 第二个 初始化 属性到一个新的 Uri
对象,并且每次都给出相同的对象。
不同的是每次都会创建一个新的Uri
实例:
public Uri MyProperty => new Uri();
这将适用于具有指定值的支持字段:
public Uri MyProperty1 { get; } = new Uri();
在 C# 6 中你可以有以下 属性:
public Uri MyProperty => new Uri();
或者你可以这样:
public Uri MyProperty1 { get; } = new Uri();
它们有什么区别?
每次访问 属性 时第一个 returns 一个新的 Uri
对象 - 第二个 初始化 属性到一个新的 Uri
对象,并且每次都给出相同的对象。
不同的是每次都会创建一个新的Uri
实例:
public Uri MyProperty => new Uri();
这将适用于具有指定值的支持字段:
public Uri MyProperty1 { get; } = new Uri();