none
Are private instance variables needed anymore? RRS feed

  • Question

  • It seems with the introduction of properties that private instance variables aren't needed anymore. Especially in the Visual Studio 2008 + IDE that auto-encapsulates everything and allows for the simple setter and getter properties.

    Can someone help me understand why I need to use the extra lines of code to create a private instance variable when a property does all the work for me?

    Thursday, October 18, 2012 9:55 PM

Answers

  • The biggest (only?) reason I ever user private backing fields is when I need to perform some type of extra logic before or after the getting or setting. 

    For instance,

    private string _privateBackingField;
    private int _getCount = 0;
    
    public string BackingField
    {
        get
        {
           _getCount++;
           return _privateBackingField;
        }
    }

    Perhaps there are other reasons, but for the most part, you are right. Auto properties are the way to go.

    • Marked as answer by Lance Stern Thursday, May 2, 2013 11:52 PM
    Friday, October 19, 2012 1:37 AM
  • In earlier version (earlier than 2008), the property must be declared only by creating a private variable.

    After 2008 is released, in order to define the property, you just dont have to define the private variable in order to access the property values.

    And thats how the syntax has been changed in 2008 or later version.

    Basically the compiler has been rewritten in order to reduce the number of lines of code.

    Public void PropertName

    {

    get;

    set;

    }

    • Marked as answer by Lance Stern Thursday, May 2, 2013 11:52 PM
    Friday, October 19, 2012 5:40 AM
  • Hi ,

    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.

    About Property VS Field, please check-out these links below:

    http://blogs.msdn.com/b/abhinaba/archive/2005/09/22/472706.aspx

    http://stackoverflow.com/questions/8773181/c-sharp-field-vs-property

    http://programmers.stackexchange.com/questions/133015/private-variable-vs-property


    Cheers, Amy

    Monday, October 22, 2012 8:44 AM

All replies

  • Do you mean public or private instance variables (fields)?

    There are a few difference between fields and properties.  In general, properties are more flexible but they also can't be used as ref or out parameters.

    Alternatively, are you talking about private instance variable in the sense of a backing store for a public property?


    jon.stromer.galley

    Thursday, October 18, 2012 11:25 PM
  • The biggest (only?) reason I ever user private backing fields is when I need to perform some type of extra logic before or after the getting or setting. 

    For instance,

    private string _privateBackingField;
    private int _getCount = 0;
    
    public string BackingField
    {
        get
        {
           _getCount++;
           return _privateBackingField;
        }
    }

    Perhaps there are other reasons, but for the most part, you are right. Auto properties are the way to go.

    • Marked as answer by Lance Stern Thursday, May 2, 2013 11:52 PM
    Friday, October 19, 2012 1:37 AM
  • Giving the property an initial value is another reason.

    string hello = "Greetings";
    public string Hello{get{return hello;} set{hello = value;}}

    Friday, October 19, 2012 2:29 AM
  • Can you show the forum which extra line that you are talking about?

    chanmm


    chanmm

    Friday, October 19, 2012 2:55 AM
  • I think what Lance is asking is

    // Why do this...
    int anInteger;
    public int AnInteger{get{return anInteger;} set{anInteger = value;}}
    
    
    // when you can do this ?
    public int AnInteger{get; set;}

    Friday, October 19, 2012 5:13 AM
  • In earlier version (earlier than 2008), the property must be declared only by creating a private variable.

    After 2008 is released, in order to define the property, you just dont have to define the private variable in order to access the property values.

    And thats how the syntax has been changed in 2008 or later version.

    Basically the compiler has been rewritten in order to reduce the number of lines of code.

    Public void PropertName

    {

    get;

    set;

    }

    • Marked as answer by Lance Stern Thursday, May 2, 2013 11:52 PM
    Friday, October 19, 2012 5:40 AM
  • Hi ,

    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.

    About Property VS Field, please check-out these links below:

    http://blogs.msdn.com/b/abhinaba/archive/2005/09/22/472706.aspx

    http://stackoverflow.com/questions/8773181/c-sharp-field-vs-property

    http://programmers.stackexchange.com/questions/133015/private-variable-vs-property


    Cheers, Amy

    Monday, October 22, 2012 8:44 AM