本文介绍了私人领域及私人性质的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的,而不是私人领域

What is the difference between using Private Properties instead of Private Fields

private String MyValue { get; set; }

// instead of

private String _myValue;

public void DoSomething()
{
   MyValue = "Test";

   // Instead of

   _myValue = "Test";
}

有没有性能问题?或者只是一个命名约定?

Is there any performance issue ? or just a naming convention ?

推荐答案

私有属性,可以抽象的内部数据,以便更改内部重新presentation不需要影响你实现的其他部分,甚至在同一个班级。私人领域不提供这种优势。在C#3.0自动属性,我很少看到有必要直接实现领域 - 私人或公共

Private properties allow you to abstract your internal data so that changes to the internal representation don't need to affect other parts of your implementation, even in the same class. Private fields do not offer this advantage. With automatic properties in C# 3.0, I rarely see a need to implement fields directly -- private or public.

这篇关于私人领域及私人性质的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 14:53