现在的位置: 首页 > 综合 > 正文

C# Field,Property,Attribute的区别

2014年09月05日 ⁄ 综合 ⁄ 共 2622字 ⁄ 字号 评论关闭

这几个词如果只看字面意思很有可能会混淆,我自己也是一直分不太清楚,因为翻译的问题,之前都是一知半解,今天专门查了查资料,整理了一下。

Attribute & Property

1.

There's a lot of difference between a C# property and attribute. A property lets you get/set data in your class.  However, an attribute allows you to decorate elements of your code
with declarative information.  i.e. here's  a class with three properties:


public class Customer
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
}

and you use it like this:

var cust = new Customer();

cust.Name = "Some Name";

Console.WriteLine("Name: " + cust.Name);

However, an attribute is much different and the attributes you use depend on the tool you are using.  For example, if you had a method you wanted to deprecate from your application, you could use the Obsolete attribute like this:

[Obsolete("Method deprecated - Please use MethB instead")]
public void MethA() { ... }

If you were writing unit tests in VS2008, you would use Test and TestMethod attributes like this:

[Test]
public class MyTestClass
{
    [TestMethod]
    public void MyTestMethod() { .... }
}

To summarize, attributes support tools that operate on your code, but properties define the state of an object and there is no logical comparison between the two.

2.

In HTML

Properties: All which either has a boolean value or that is UA calculated such as selectedIndex.

Attributes: 'Attributes' that can be added to a HTML element which is neither boolean nor containing a UA generated value.

Field & Property

1.

A field gives calling code direct access to the data in your class, but a property allows you to control access to that value.  

If the underlying representation of the data changes, then a field would cause calling code to break, which could cause a rippling effect through your code for trying to fix the change.  If someone else's code
depends on that field, then their code would break, which means that you might not be able to change your class.  If you use a property, you can manipulate the representation of the data in the property accessers if there ever was a change and calling code
would never know, which gives you more maintainable code.  

2.

Object orientated programming principles say that, the internal workings of a class should be hidden from the outside world. If you expose a field you're in essence exposing the
internal implementation of the class. Therefore we wrap fields with Properties (or methods in Java's case) to give us the ability to change the implementation without breaking code depending on us.

3.

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a
level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

4.

for me a property is something that you can add ( if you want to) business logic( like DateOfBirth can not occur in the future). Of course, this
is realized with getter/setter functions
A field is some string that describes an "property" for the object ( for example, the name of the user).



抱歉!评论已关闭.