开发者

why and when to use properties

I am very confused with properties in asp.net. I just don't understand why we use properties and when I should use them. Could anybody elaborate a little on this.

public class Customer
{
    private int m_id = -1;

    public int ID
    {
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        set
        {
            m_name = value;
        }
开发者_运维知识库    }

    public void DisplayCustomerData()
    {
        Console.WriteLine("ID: {0}, Name: {1}", m_id, m_name);
    }
}


Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is often accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field.

Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code. Now, lets see how to use properties.

Taken From CSharp-Station


There are a couple of good reasons for it. The first is that you might need to add validation logic in your setter, or actually calculate the value in the getter.

Another reason is something to do with the IL code generated. If you are working on a large project that is spread over multiple assemblies then you can change the code behind your property without the application that uses your assembly having to recompile. This is because the "access point" of the property stays the same while allowing the implementation code behind it to be altered. I first read about this when I was looking into the point of automatic properties as I didnt see the point between those and a normal public variable.


It's easy. All fields in class MUST be private (or protected). To show fields to another class yyou can use properties or get/set methods. Properties a shorter.

P.S. Don't declare write-only properties. It is worst practices.


Properties are a convenient way to encapsulate your classes' data. Quoting from 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.

Let's consider two common scenarios:

1) You want to expose the Name property without making it changeable from outside the class:

  private string m_name = string.Empty;
  public string Name
  {
      get
      {
         return m_name;
      }
  }

2) You want to perform some checks, or run some code every time the data is accessed or set:

  private string m_name = string.Empty;
  public string Name
  {
      get
      {
         return m_name;
      }
      set
      {
         m_name = (String.IsNullOrEmpty(value)) ? "DefaultName" : value;
      }
  }

see: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx


The most important reason is for validation purpose in setter and manipulation part can be implemented in get part. For Ex. Storing weekdays, which should be from 1-7, if we take normal variable and declare it as public, anyone can assign any value. But in Properties setter you can control and validate.

The next one you can use it for tracking. That means, you can know how many times set and get functions has been called by clients (statistical purpose, may be not useful frequently).

Finally, you can control read only, write only and read/write for the properties according to your requirements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜