Abhishek Anand

Bits & Bytes about .net technology framework.
posts - 29 , comments - 3 , trackbacks - 0

Properties vs. Methods

One of the decisions a developer needs to make while designing a class is when to use a property and when to use a method. Methods typically represent an action or an operation whereas, properties represent pieces of data associated with a class / instance. Properties are typically used to allow accessibility (getting and/or setting) to private class fields.

 

Cases where the use of a property is recommended:

Ø  When the member represents a logical attribute of the type.

E.g. Message is a property of class Exception because the message of an exception is an attribute that describes that exception.

Ø  When the value of the property is stored in the process memory and the property would just provide access to the value. For Example:

public class StudentRecord

{

    private int studentId;           

    public int StudentId

    {

        get {return studentId;}

        set {studentId = value;}

    }

}

 

Cases where the use of a method is recommended:

Ø  The operation is much slower than a field set would be. For example, operations that access the network or the file system (other than once for initialization) should most likely be methods, not properties.

Ø  The operation is a conversion, such as the Object.ToString method.

Ø  The operation returns a different result each time it is called, even if the parameters do not change. For example, the NewGuid method returns a different value each time it is called.

Ø  The operation returns an array. The reason is:

 

In order to protect the contents of an internal array from being altered by users, you would need to return a deep copy of the array, not a reference to the array used by the property (check the previous topic – Usage of Arrays). This fact combined with the fact that developers typically use properties as though they were fields, can lead to very inefficient code. This is illustrated in the following code example, which returns an array using a property (bad practice).

public class StudentData

{

    StudentRecord[] data;

    public StudentData(StudentRecord[] data)

    {

        this.data = data;

    }

    public StudentRecord[] Students

    {

        get

        {

            StudentRecord[] newData = CopyStudentRecords();

            return newData;

        }

    }

    StudentRecord[] CopyStudentRecords()

    {

        StudentRecord[] newData = new StudentRecord[data.Length];

        for(int i = 0; i< data.Length; i++)

        {

            newData[i] = data[i].Clone();

        }

        Console.WriteLine ("StudentData: cloned student data.");

        return newData;

    }

}

 

A developer using this class assumes that the property is no more expensive than a field access and writes application code based on that assumption resulting in a significant impact on performance.

Notice

Each time the Students property is accessed, a copy of data (of type StudentRecord[]) is created, used briefly, and then requires garbage collection. By implementing Students as a method, you indicate to developers that this action is more computationally expensive than accessing a field. Developers are more likely to call a method once and cache the results of the method call to perform their processing.

Print | posted on Wednesday, November 20, 2013 4:31 PM | Filed Under [ .net c# methods properties ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: