C# 6 中(自动)属性初始化语法之间的区别
Difference between (auto) properties initialization syntax in C# 6
在 C# 6 中初始化属性的下列表达式有什么区别:
1. Auto-属性 从构造函数初始化
public class Context1
{
public Context1()
{
this.Items = new List<string>();
}
public List<string> Items { get; private set; }
}
2:属性 从支持字段初始化
public class Context2
{
private readonly List<string> items;
public Context2()
{
this.items = new List<string>();
}
public List<string> Items
{
get
{
return this.items;
}
}
}
3: Auto-属性 C# 6 中的新语法
public class Context3
{
public List<string> Items { get; } = new List<string>();
}
4: Auto-属性 C# 6 中的新语法
public class Context4
{
public List<string> Items => new List<string>();
}
清单 3 是 C# 6 的等效清单 2,其中在后台提供了支持字段。
清单 4:
public List<string> Items => new List<string>();
相当于:
public List<string> Items { get { return new List<string>(); } }
正如您想象的那样 returns 每次访问 属性 时都会生成一个新的空列表。
列表 2/3 和 4 之间的区别在 中通过示例进一步探讨。
清单 1 只是一个带有 getter 和私有 setter 的自动 属性。它不是只读的 属性,因为您可以将其设置在任何可以访问该类型的任何私有成员的位置。只读 属性(即 getter-only 属性)可以 only 在构造函数或 属性 中初始化] 声明,很像只读字段。
Auto-属性 是 auto-implemented 属性 的简称,开发人员不不需要显式声明支持字段,编译器会在后台设置一个。
1。自动 属性 与私人 setter
public class Context1
{
public Context1()
{
this.Items = new List<string>();
}
public List<string> Items { get; private set; }
}
自动属性可以为 setter 和 getter 提供不同的可访问性,方法是为可访问性与 属性 的可访问性不同的访问器指定更具限制性的可访问性。
其他例子是:
public string Prop1 { get; private set; }
public string Prop2 { get; protected set; }
public string Prop3 { get; internal set; }
public string Prop4 { protected internal get; set; }
可以在可访问性决定的任何地方访问这些具有不同可访问性的访问器,而不仅仅是从构造函数。
2。只读 属性 带有支持字段
public class 上下文 2
{
私有只读列表项;
public Context2()
{
this.items = new List<string>();
}
public List<string> Items
{
get { return this.items; }
}
}
在 C#6 之前,设置只读 属性 值的唯一方法是显式声明支持字段并直接设置它。
因为字段有readonly
访问器,只能在对象构造时设置。
3。只读自动-属性
public class Context3
{
public List<string> Items { get; } = new List<string>();
}
从 C#6 开始,§2 可以由编译器处理,方法是像读写自动属性一样生成一个支持字段,但在这种情况下,支持字段是只读的,只能在对象构造期间设置。
4。只读 Auto-属性 with expression bodied getter
public class Context4
{
public List<string> Items => new List<string>();
}
当属性的值在每次获取时都会更改,C#6 允许使用 声明 getter 的主体类似 lambda 的 语法。
上面的代码等同于:
public class Context4
{
public List<string> Items
{
get { return new List<string>(); }
}
}
在 C# 6 中初始化属性的下列表达式有什么区别:
1. Auto-属性 从构造函数初始化
public class Context1
{
public Context1()
{
this.Items = new List<string>();
}
public List<string> Items { get; private set; }
}
2:属性 从支持字段初始化
public class Context2
{
private readonly List<string> items;
public Context2()
{
this.items = new List<string>();
}
public List<string> Items
{
get
{
return this.items;
}
}
}
3: Auto-属性 C# 6 中的新语法
public class Context3
{
public List<string> Items { get; } = new List<string>();
}
4: Auto-属性 C# 6 中的新语法
public class Context4
{
public List<string> Items => new List<string>();
}
清单 3 是 C# 6 的等效清单 2,其中在后台提供了支持字段。
清单 4:
public List<string> Items => new List<string>();
相当于:
public List<string> Items { get { return new List<string>(); } }
正如您想象的那样 returns 每次访问 属性 时都会生成一个新的空列表。
列表 2/3 和 4 之间的区别在
清单 1 只是一个带有 getter 和私有 setter 的自动 属性。它不是只读的 属性,因为您可以将其设置在任何可以访问该类型的任何私有成员的位置。只读 属性(即 getter-only 属性)可以 only 在构造函数或 属性 中初始化] 声明,很像只读字段。
Auto-属性 是 auto-implemented 属性 的简称,开发人员不不需要显式声明支持字段,编译器会在后台设置一个。
1。自动 属性 与私人 setter
public class Context1
{
public Context1()
{
this.Items = new List<string>();
}
public List<string> Items { get; private set; }
}
自动属性可以为 setter 和 getter 提供不同的可访问性,方法是为可访问性与 属性 的可访问性不同的访问器指定更具限制性的可访问性。
其他例子是:
public string Prop1 { get; private set; }
public string Prop2 { get; protected set; }
public string Prop3 { get; internal set; }
public string Prop4 { protected internal get; set; }
可以在可访问性决定的任何地方访问这些具有不同可访问性的访问器,而不仅仅是从构造函数。
2。只读 属性 带有支持字段
public class 上下文 2 { 私有只读列表项;
public Context2()
{
this.items = new List<string>();
}
public List<string> Items
{
get { return this.items; }
}
} 在 C#6 之前,设置只读 属性 值的唯一方法是显式声明支持字段并直接设置它。
因为字段有readonly
访问器,只能在对象构造时设置。
3。只读自动-属性
public class Context3
{
public List<string> Items { get; } = new List<string>();
}
从 C#6 开始,§2 可以由编译器处理,方法是像读写自动属性一样生成一个支持字段,但在这种情况下,支持字段是只读的,只能在对象构造期间设置。
4。只读 Auto-属性 with expression bodied getter
public class Context4
{
public List<string> Items => new List<string>();
}
当属性的值在每次获取时都会更改,C#6 允许使用 声明 getter 的主体类似 lambda 的 语法。
上面的代码等同于:
public class Context4
{
public List<string> Items
{
get { return new List<string>(); }
}
}