c# 将字段与 get 和 set 方法结合使用与使用 属性
c# using field with get and set methods vs using property
使用具有 get 和 set 方法的字段与使用 属性 通过 class 将值赋予对象之间在功能上有什么区别?例如,在 class 中设置值 val
时,是否有任何理由选择下面的两个 class 之一而不是另一个(除了编写的代码长度和界面兼容性):
class FieldTest
{
public FieldTest()
{
}
private string val;
public void SetVal(string temp)
{
val = temp;
}
public string GetVal()
{
return val;
}
}
对比
class PropertyTest
{
public PropertyTest()
{
}
public string val { get; set; }
}
2010 年 Visual Studio 的测试使用情况:
class TestFunctions
{
static void Main(string[] args)
{
FieldTest Test_Fields = new FieldTest();
Test_Fields.SetVal("Test");
string temp_str = Test_Fields.GetVal();
PropertyTest Test_Property = new PropertyTest();
Test_Property.val = "test";
string temp_str_prop = Test_Property.val;
System.Windows.Forms.MessageBox.Show("Field: " + temp_str + "\n\nProperty: " + temp_str_prop);
}
}
我知道只有一个字段可以使用ref
和out
关键字,但其他优点通常归因于 属性——封装、版本控制等——这两个设置似乎是相同的。
我查看了 Difference between Property and Field in C# 3.0+ and What is the difference between a Field and a Property in C#? 等文章。尽管他们很好地描述了属性和字段背后的思想,但我无法找到我的问题的具体答案。
提前感谢您的澄清。
编辑 2015-07-29:
我认为这是一个独立于其他 Whosebug 答案的问题,例如 here,因为这些答案似乎没有专门解决使用具有自己的 get 和 set 方法的字段来替代 属性.
我上面的声明,“我知道只有一个字段可以使用 ref
和 out
关键字。 .." 来自类似于以下的答案(发现 here):
“字段可用于 out/ref 参数,属性不能。属性支持额外的
逻辑——这可以用来实现延迟加载等等。”
在 .NET 世界中 properties
是将数据归因于对象的方式。 Methods
通常是与对象关联的操作。 Fields
通常存储内部(私有)对象实例状态。
在幕后,read/write 属性 访问器被编译为获取和设置方法。
此外,许多技术不适用于方法。数据注释、Entity Framework 和序列化是一些立即浮现在脑海中的东西。
功能几乎相同。对于 "normal" 代码用例,这些片段的作用完全相同,因为 属性 实际上只是一个具有两个隐藏方法(get
和 set
)的隐藏字段.
但是, 在反射方面存在差异。属性显示为 PropertyInfo
,方法显示为 MethodInfo
。您也只能绑定到属性(在 WPF/WinRT 中)。序列化也只适用于属性。这两个(毫无疑问还有其他)都失败了,因为它们使用反射来找到要反对的成员。
因此,根据您的用例,它们是相同的。一般来说,我会坚持属性。
我总是会投票给属性而不是 getter
和 setter
。
首先-使用属性是整洁干净的。代码更清晰,更简洁,更容易理解。
如果你使用Automatic 属性你只需要一行代码一个属性 其中 getter
和 setter
方法至少需要 6 个。那么,如果您的 class 有 20 个属性,那么总共需要 120 行代码?天呐!!!
但是通常归因于 属性 的其他优点——封装、版本控制等——似乎与这两种设置相同。 => 我不同意,考虑一个场景,你想强制所有 interface
的实现,属性为 readonly
。在 interface
中使用 readonly
属性 很容易做到这一点。现在尝试使用 getter
和 setter
。坦率地说,你不能。
然后是序列化。您不能序列化计算值,除非它是 属性。方法从不序列化。
让我们看一下您的第二个代码:
class PropertyTest
{
public PropertyTest()
{
}
public string val { get; set; }
}
如 Auto-Implemented Properties page on MSDN 中所述,当您像示例中那样声明自动实现的 属性 时,编译器会创建一个私有的匿名支持字段,只能通过 属性 的获取和设置访问器。
换句话说,就像这样写代码:
public class PropertyTest
{
public PropertyTest()
{
}
private string _val;
public string val
{
get { return _val; }
set { val = value; }
}
}
因此,属性是封装字段的一种方式。正如您在 MSDN 上看到的那样:
A property is a member that provides a flexible mechanism to read,
write, or compute the value of a private field. Properties can be used
as if they are public data members, but they are actually special
methods called accessors. This enables data to be accessed easily and
still helps promote the safety and flexibility of methods.
在我看来,与 getter/setter 方法相比,您应该始终更喜欢使用 属性 实现。并不是因为计算值之类的东西看起来更干净、更灵活,而是它实际上更容易实现(你在自动实现的属性上写的代码更少)。
如果我们看一下 MSDN 说 "but they are actually special methods called accessors" 的部分,我们可以说与 getter/setter 方法的属性几乎没有区别。但是同样,我们有上面无脑编码器的例子,我们有鼓励我们使用属性的框架行为:方法不能序列化,而属性可以。
使用具有 get 和 set 方法的字段与使用 属性 通过 class 将值赋予对象之间在功能上有什么区别?例如,在 class 中设置值 val
时,是否有任何理由选择下面的两个 class 之一而不是另一个(除了编写的代码长度和界面兼容性):
class FieldTest
{
public FieldTest()
{
}
private string val;
public void SetVal(string temp)
{
val = temp;
}
public string GetVal()
{
return val;
}
}
对比
class PropertyTest
{
public PropertyTest()
{
}
public string val { get; set; }
}
2010 年 Visual Studio 的测试使用情况:
class TestFunctions
{
static void Main(string[] args)
{
FieldTest Test_Fields = new FieldTest();
Test_Fields.SetVal("Test");
string temp_str = Test_Fields.GetVal();
PropertyTest Test_Property = new PropertyTest();
Test_Property.val = "test";
string temp_str_prop = Test_Property.val;
System.Windows.Forms.MessageBox.Show("Field: " + temp_str + "\n\nProperty: " + temp_str_prop);
}
}
我知道只有一个字段可以使用ref
和out
关键字,但其他优点通常归因于 属性——封装、版本控制等——这两个设置似乎是相同的。
我查看了 Difference between Property and Field in C# 3.0+ and What is the difference between a Field and a Property in C#? 等文章。尽管他们很好地描述了属性和字段背后的思想,但我无法找到我的问题的具体答案。
提前感谢您的澄清。
编辑 2015-07-29:
我认为这是一个独立于其他 Whosebug 答案的问题,例如 here,因为这些答案似乎没有专门解决使用具有自己的 get 和 set 方法的字段来替代 属性.
我上面的声明,“我知道只有一个字段可以使用 ref
和 out
关键字。 .." 来自类似于以下的答案(发现 here):
“字段可用于 out/ref 参数,属性不能。属性支持额外的
逻辑——这可以用来实现延迟加载等等。”
在 .NET 世界中 properties
是将数据归因于对象的方式。 Methods
通常是与对象关联的操作。 Fields
通常存储内部(私有)对象实例状态。
在幕后,read/write 属性 访问器被编译为获取和设置方法。
此外,许多技术不适用于方法。数据注释、Entity Framework 和序列化是一些立即浮现在脑海中的东西。
功能几乎相同。对于 "normal" 代码用例,这些片段的作用完全相同,因为 属性 实际上只是一个具有两个隐藏方法(get
和 set
)的隐藏字段.
但是, 在反射方面存在差异。属性显示为 PropertyInfo
,方法显示为 MethodInfo
。您也只能绑定到属性(在 WPF/WinRT 中)。序列化也只适用于属性。这两个(毫无疑问还有其他)都失败了,因为它们使用反射来找到要反对的成员。
因此,根据您的用例,它们是相同的。一般来说,我会坚持属性。
我总是会投票给属性而不是 getter
和 setter
。
首先-使用属性是整洁干净的。代码更清晰,更简洁,更容易理解。
如果你使用Automatic 属性你只需要一行代码一个属性 其中
getter
和setter
方法至少需要 6 个。那么,如果您的 class 有 20 个属性,那么总共需要 120 行代码?天呐!!!但是通常归因于 属性 的其他优点——封装、版本控制等——似乎与这两种设置相同。 => 我不同意,考虑一个场景,你想强制所有
interface
的实现,属性为readonly
。在interface
中使用readonly
属性 很容易做到这一点。现在尝试使用getter
和setter
。坦率地说,你不能。然后是序列化。您不能序列化计算值,除非它是 属性。方法从不序列化。
让我们看一下您的第二个代码:
class PropertyTest
{
public PropertyTest()
{
}
public string val { get; set; }
}
如 Auto-Implemented Properties page on MSDN 中所述,当您像示例中那样声明自动实现的 属性 时,编译器会创建一个私有的匿名支持字段,只能通过 属性 的获取和设置访问器。
换句话说,就像这样写代码:
public class PropertyTest
{
public PropertyTest()
{
}
private string _val;
public string val
{
get { return _val; }
set { val = value; }
}
}
因此,属性是封装字段的一种方式。正如您在 MSDN 上看到的那样:
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
在我看来,与 getter/setter 方法相比,您应该始终更喜欢使用 属性 实现。并不是因为计算值之类的东西看起来更干净、更灵活,而是它实际上更容易实现(你在自动实现的属性上写的代码更少)。
如果我们看一下 MSDN 说 "but they are actually special methods called accessors" 的部分,我们可以说与 getter/setter 方法的属性几乎没有区别。但是同样,我们有上面无脑编码器的例子,我们有鼓励我们使用属性的框架行为:方法不能序列化,而属性可以。