locked
Why do we need properties RRS feed

  • Question

  • Hi All,

    This is question may look very simple for Techies here..

    I was wondering why do we need properties, while we can access the variable directly by specifies the access specifier for them.

    i wish to know, what was the need to use properties and what we achieve by using them. i mean pros and cons ( if we don't use) 

    Thanks for reading my post and replying

    Thursday, March 25, 2010 5:32 AM

Answers

  • Hi, chezhian_in05:

         Thanks for your posting. No question is too simple to ask, as long as it is posted to the proper forum.  :)

         In fact, the Common Language Runtime (CLR) offers two kinds of properties: parameterless properties, which I believe is your case, and parameterful properties, which are called indexers in C#, and default properties in VB.NET.

         Tyes usually store their state informations in their field members. For example:

    public class Person
    {
       public String name;
       public Int32 age;
    }

         You can assign or modify an object's state by accessing their public fields directly. However, the preceding code should be never implemented that way. One of the concern of object-oriented design is data encapsulation. Data encapsulation means your type's field should never be publicly exposed because it's too easy to write code that improperly uses the fields and thus corrupt the object's internal state. For example:

    Person p;
    p.age = -5;

         Also, there're addtional reasons for encapsulating access to a type's data fields. For example, you might want some side effects to happen when data fields are accessed, cache some value, or lazily create some internal object. You might also want the data field to be accessed in a thread-safe manner. Or perhaps the field is a logical field whose value is calcualted using some algorithm rather than represented by some data fields defined within the type.

         For any of these reasons, it is good manner to define fields as private, and access them through methods. These methods are typically called accessor methods. For example:

    public class Person
    {
       private String name;
       private Int32 age;
    
       public String GetName()
       {
           return name;
       }
    
       public Int32 GetAge()
       {
           return age;
       }
    }

         But the above code could be tedious and we must get our field via method calls. And that's just not good enough. So CLR offer a mechanism called properties to save our key strokes and make it look like the good old public field, as below:

    public class Person
    {
       private String m_name;
       private Int32 m_age;
    
       public String Name
       {
           get { return m_name; }
           set { m_name = value;}
       }
    
       public Int32 GetAge()
       {
           get { return m_age; }
           set { m_age = value;}      
       }
    }

         So, that's the parameterless property. For parameterful property, see here.

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, March 25, 2010 7:40 AM refine
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 7:37 AM
  • a) you can define different scope for getter and setter.

    b) you can place code within getter and setter


    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 7:22 AM
  • SamAgain provided a great answer which summarizes most of the properties benefits. I would only want to add that when you're designing a class you might feel you don't need to lazy load anything, to do any checks, sideeffects, etc... Even in those cases you should also create properties for your member fields, because in the future you might need to add those behaviors and if you hadn't created properties in the first place you'll end up not only adding that behavior but changing every piece of code that accesses it! Properties are also valuable in the sense that they provide you this agility of adding small localized behaviors without major changes across your application.

    About Cons of using properties, there really isn't much to mention, you might argue, that accessing a member value is slower because it's done through a method, instead of accessing the memory directly, but that's unnecessary micro-optimization. The pros largely outweigh the cons!


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 9:19 AM

All replies

  • a) you can define different scope for getter and setter.

    b) you can place code within getter and setter


    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 7:22 AM
  • Hi, chezhian_in05:

         Thanks for your posting. No question is too simple to ask, as long as it is posted to the proper forum.  :)

         In fact, the Common Language Runtime (CLR) offers two kinds of properties: parameterless properties, which I believe is your case, and parameterful properties, which are called indexers in C#, and default properties in VB.NET.

         Tyes usually store their state informations in their field members. For example:

    public class Person
    {
       public String name;
       public Int32 age;
    }

         You can assign or modify an object's state by accessing their public fields directly. However, the preceding code should be never implemented that way. One of the concern of object-oriented design is data encapsulation. Data encapsulation means your type's field should never be publicly exposed because it's too easy to write code that improperly uses the fields and thus corrupt the object's internal state. For example:

    Person p;
    p.age = -5;

         Also, there're addtional reasons for encapsulating access to a type's data fields. For example, you might want some side effects to happen when data fields are accessed, cache some value, or lazily create some internal object. You might also want the data field to be accessed in a thread-safe manner. Or perhaps the field is a logical field whose value is calcualted using some algorithm rather than represented by some data fields defined within the type.

         For any of these reasons, it is good manner to define fields as private, and access them through methods. These methods are typically called accessor methods. For example:

    public class Person
    {
       private String name;
       private Int32 age;
    
       public String GetName()
       {
           return name;
       }
    
       public Int32 GetAge()
       {
           return age;
       }
    }

         But the above code could be tedious and we must get our field via method calls. And that's just not good enough. So CLR offer a mechanism called properties to save our key strokes and make it look like the good old public field, as below:

    public class Person
    {
       private String m_name;
       private Int32 m_age;
    
       public String Name
       {
           get { return m_name; }
           set { m_name = value;}
       }
    
       public Int32 GetAge()
       {
           get { return m_age; }
           set { m_age = value;}      
       }
    }

         So, that's the parameterless property. For parameterful property, see here.

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, March 25, 2010 7:40 AM refine
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 7:37 AM
  • SamAgain provided a great answer which summarizes most of the properties benefits. I would only want to add that when you're designing a class you might feel you don't need to lazy load anything, to do any checks, sideeffects, etc... Even in those cases you should also create properties for your member fields, because in the future you might need to add those behaviors and if you hadn't created properties in the first place you'll end up not only adding that behavior but changing every piece of code that accesses it! Properties are also valuable in the sense that they provide you this agility of adding small localized behaviors without major changes across your application.

    About Cons of using properties, there really isn't much to mention, you might argue, that accessing a member value is slower because it's done through a method, instead of accessing the memory directly, but that's unnecessary micro-optimization. The pros largely outweigh the cons!


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by chezhian_in05 Thursday, March 25, 2010 9:30 AM
    Thursday, March 25, 2010 9:19 AM
  • Thank you Miha Markic and SamAgain and Bruno Coelho Marques .

    SamAgain. The explanation was in detail enough for me to understand. Thanks for that.

    public
     class
     Person
    {
    private String name;
    private Int32 age;

    public String GetName()
    {
    return name;
    }

    public Int32 GetAge()
    {
    return age;
    }
    }

    I've another question for SamAgain .. In the explanation you've told , This could be tedious.

    bit curious to know about that.  Can you brief about the effects.

    Thanks for all your time

    Thursday, March 25, 2010 9:23 AM
  • Hi, chezhian_in05:

         By tedious I mean that there's just too much typing~ :)


    Please mark the right answer at right time.
    Thanks,
    Sam
    Thursday, March 25, 2010 9:27 AM
  • Thank you SamAgain..

    Thank you guy for all your time..

     

    It's time to close this tread. Marking the responses as Answer.

    Thursday, March 25, 2010 9:29 AM