什么是在C#中使用私有变量的优势 [英] what is the advantage of using private variables in C#
问题描述
示例代码(替代代码如下),
// person.cs
使用系统;
类Person
{
私人字符串MYNAME =N / A;
//声明string类型的Name属性:
公共字符串名称
{
得到
{
返回MYNAME;
}
组
{
MYNAME =价值;
}
}
公共重写字符串的ToString()
{
返回NAME =+姓名;
}
公共静态无效的主要()
{
的人的人=新的Person();
Console.WriteLine(人的细节 - {0}的人);
person.Name =乔;
Console.WriteLine(人的细节 - {0}的人);
}
}
我们不能直接写,改变 MYNAME
从私有到公有,不要求声明另一个公共变量名称,也没有必要使用get和set?
的替代代码的
// person.cs
使用系统;
类Person
{
公共字符串MYNAME =N / A;
公共重写字符串的ToString()
{
返回NAME =+ MYNAME;
}
公共静态无效的主要()
{
的人的人=新的Person();
Console.WriteLine(人的细节 - {0}的人);
person.myName =乔;
Console.WriteLine(人的细节 - {0}的人);
}
}
外部可见性能均优于领域更好,因为:
-
属性以便更好地封装。字段是一个固定的执行情况,并允许消费者直接访问。属性:
-
松散耦合(因为基础字段可以从变量更改为
数据库随时) -
允许自定义逻辑(验证,事件通知,延迟加载等。)
-
访问控制(因为逻辑可以的get / set建成,甚至宣称只读或只写)。
-
-
字段不能在接口<使用/ STRONG>。这是测试驱动阻碍发展(接口在前)。
-
自动或自动实现的属性是容易以声明领域,也进行了优化与领域看齐执行。请参见 href=\"http://msdn.microsoft.com/en-us/library/bb384054.aspx\">。
-
声明一个外部可见的领域(公共,保护,受保护的内部)是一种的FxCop冲突即可。请参见规则CA1051 。
-
更改字段属性是一个破更改,因为调用代码需要重新编译(适用于二进制序列化以及)。
-
属性为认可在.NET许多图书馆的任务,如XML序列化,绑定WPF,ASP.NET 2路绑定等,并也是由Visual Studio设计。
Sample code (alternative code is below),
// person.cs
using System;
class Person
{
private string myName ="N/A";
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
public override string ToString()
{
return "Name = " + Name;
}
public static void Main()
{
Person person = new Person();
Console.WriteLine("Person details - {0}", person);
person.Name = "Joe";
Console.WriteLine("Person details - {0}", person);
}
}
Can't we directly write, changing myName
from private to public, no requirement to declare another public variable Name and no need to use get and set?
alternative code
// person.cs
using System;
class Person
{
public string myName ="N/A";
public override string ToString()
{
return "Name = " + myName;
}
public static void Main()
{
Person person = new Person();
Console.WriteLine("Person details - {0}", person);
person.myName = "Joe";
Console.WriteLine("Person details - {0}", person);
}
}
Externally visible properties are better than fields because:
Properties allow better encapsulation. Fields are a fixed implementation and allow direct access from consumers. Properties:
are loosely coupled (since underlying field can change from variable to database anytime)
allow custom logic (validation, event notification, lazy loading, etc.)
control access (since logic can be built in get/set, even declared read-only or write-only).
Fields cannot be used in interfaces. This is an impediment to Test Driven Development (interface first).
Automatic or Auto-Implemented Properties are as easy to declare as fields and also optimized to perform on par with fields. See here.
Declaring an externally visible field (public, protected, protected internal) is a FxCop violation. See rule CA1051.
Changing a field to a property is a breaking change, since the calling code needs to be recompiled (applies to binary serialization as well).
Properties are recognized by many libraries in .NET for tasks such as XML serialization, WPF bindings, ASP.NET 2-way binding, etc. and also by Visual Studio designer.
这篇关于什么是在C#中使用私有变量的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!