C# 自动封装字段重构,区别于';使用字段';和';使用属性';?
在Visual Studio 2017上,当使用自动封装字段重构工具时,我有两个选项:C# 自动封装字段重构,区别于';使用字段';和';使用属性';?,c#,visual-studio,C#,Visual Studio,在Visual Studio 2017上,当使用自动封装字段重构工具时,我有两个选项: 使用属性 仍然使用字段 我在一个基本类上测试了不同的选项: public class Test_EncapsulateFieldRefactoring_Property { public int id; public string name; } 但两种选择都给出了相同的结果: public class Test_EncapsulateFieldRefactoring_Property {
public class Test_EncapsulateFieldRefactoring_Property
{
public int id;
public string name;
}
但两种选择都给出了相同的结果:
public class Test_EncapsulateFieldRefactoring_Property
{
private int id;
private string name;
public int Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
}
为什么会有这些选择?差异在哪里(在生成的代码中,“useage”*)
Disclamer:
- 屏幕截图是关于法语和法语的,所以选项翻译是我自己做的,实物期权文本可能会有所不同
- 我知道土地和财产的区别。我检查了很多主题,看看这是否是一个骗局。我可能错过了一个李>
- *,找不到一个好的翻译:“在你使用它的方式”。但在此上下文中,as字段和property之间的用法不同,而是在菜单选项中
- 封装字段(并使用属性)
- 封装字段(但仍使用字段)
- 在英语中,这些选项称为:
var test = new Test_EncapsulateFieldRefactoring_Property();
test.name = "Hello";
您会发现第一个选项将
test.name
更新为新的test.name
属性,但第二个选项没有更新。在英语中,这些选项称为:
- 封装字段(并使用属性)
- 封装字段(但仍使用字段)
var test = new Test_EncapsulateFieldRefactoring_Property();
test.name = "Hello";
您会发现第一个选项将test.name
更新为新的test.name
属性,但第二个选项没有