locked
Encapsulation ..... RRS feed

  • Question

  • Hi

          I have got a private field named test. When I right click the field and select Refractor -> Encapsulate field,

    a new property is created. What is the difference between accessing a variable through a property and accessing it by making the field public. i.e.,

    1.   public string test;

          ( or )

    2.
    string test;

    public string Test
    {
         get { return test; }
         set  { test = value;  }
     }

    What is the difference between accessing in the above two ways.

    Thanks,
    Sanjay.
    Wednesday, December 24, 2008 3:38 PM

Answers

  • //Example 1 
     
    private int myInt; 
     
    public int MyInt 
         get { return myInt; } //to provide read only access 
     
    //Example 2 
     
    private int myInt; 
     
    public int MyInt 
         get { return myInt; }  
         set  
         {  
              if (value > 0 && value < 10) //restricting invalid values 
              { 
                   myInt = value; 
              } 
         }  
     
    encapsulation is a good programming pratice.. it is helpful in cases whereby you need to restrict the developer to assigning some invalid values..
    the way you are doing does not really require property but it is recommended that you use them .. in c# 3.0 you can declare properties with full access (just get and set ) easily by using public MyInt { get; set; } this way you dont have to declare another field then using a property to wrap it, however you cant modify set or get with this method as well.. 

    if you are worried about performance..  the compiled actually optimizes your code in the release build if a field does not really need property to access


    Heaven's Path - Please mark the post as answered if it is what u needed!
    • Proposed as answer by Harry Zhu Friday, December 26, 2008 7:27 AM
    • Marked as answer by Harry Zhu Wednesday, December 31, 2008 2:15 AM
    Wednesday, December 24, 2008 3:53 PM
  • the answer is always it depends, I dont think it makes any difference in some cases, where you want a variable exposed. they call it bad practice but you can always easily refactor to make it a property later when needed, but that's just me.
    Please click 'Mark as Answer' on the post that helped you.
    • Proposed as answer by Harry Zhu Friday, December 26, 2008 7:27 AM
    • Marked as answer by Harry Zhu Wednesday, December 31, 2008 2:15 AM
    Wednesday, December 24, 2008 5:27 PM

All replies

  • Hello msanjayv,
        Although I'm not positive, I'm pretty sure that the second one, will allow other forms to access the test variable, while the first will not.

    Hope this helps!
    Thanks!
    chukrum47
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    • Proposed as answer by Harry Zhu Friday, December 26, 2008 7:27 AM
    • Unproposed as answer by Harry Zhu Friday, December 26, 2008 7:28 AM
    Wednesday, December 24, 2008 3:48 PM
  • //Example 1 
     
    private int myInt; 
     
    public int MyInt 
         get { return myInt; } //to provide read only access 
     
    //Example 2 
     
    private int myInt; 
     
    public int MyInt 
         get { return myInt; }  
         set  
         {  
              if (value > 0 && value < 10) //restricting invalid values 
              { 
                   myInt = value; 
              } 
         }  
     
    encapsulation is a good programming pratice.. it is helpful in cases whereby you need to restrict the developer to assigning some invalid values..
    the way you are doing does not really require property but it is recommended that you use them .. in c# 3.0 you can declare properties with full access (just get and set ) easily by using public MyInt { get; set; } this way you dont have to declare another field then using a property to wrap it, however you cant modify set or get with this method as well.. 

    if you are worried about performance..  the compiled actually optimizes your code in the release build if a field does not really need property to access


    Heaven's Path - Please mark the post as answered if it is what u needed!
    • Proposed as answer by Harry Zhu Friday, December 26, 2008 7:27 AM
    • Marked as answer by Harry Zhu Wednesday, December 31, 2008 2:15 AM
    Wednesday, December 24, 2008 3:53 PM
  • the answer is always it depends, I dont think it makes any difference in some cases, where you want a variable exposed. they call it bad practice but you can always easily refactor to make it a property later when needed, but that's just me.
    Please click 'Mark as Answer' on the post that helped you.
    • Proposed as answer by Harry Zhu Friday, December 26, 2008 7:27 AM
    • Marked as answer by Harry Zhu Wednesday, December 31, 2008 2:15 AM
    Wednesday, December 24, 2008 5:27 PM