public data members vs. properties

what's the real difference between public data member and properties


Answer this question

public data members vs. properties

  • cp14feb

    Properties are in place to encapsulate the members variables of the classes.

    Following are some merits and demerits:

    Advantages are:

    1. Encapsulation gives you the ability to hide the member variables from other classes.
    2. Perform your custom logic while trying to access the property a can be done through the code bocks in get and set of the property.

    a this may be necessary sometimes where you need to return a calculated value or return diff values based on diff conditions

    Disadvantages:

    1. As someone mentioned before, this encapsulation can be expensive at times, since you don’t know what the property is doing underneath..

    2. All properties are internally a method which are marked as get_PropertyName and set_PropertyName. Accessing a data member would always be a little faster than accessing through the function.

    PS: I guess the Codes for the same are already posted by many.

    HTH,

    Prakash




  • Marc L. K.

    Just in case, I would check to make sure all of your references are correct.

    The difference between Itemset and itemSet is very small indeed.

  • Christian13

    Another difference is that a property does not have to be a variable. It can return the result of a calculation, etc. IMO this is dangerous, you never know the cost of accessing a property. However, properties that are not lightweight would reflect bad design, also.

    So, the reasons to use properties are:

    being able to expose get without set

    being able to run other code, another example would be ease of setting a breakpoint or running some code whenever a property is accessed

    being able to set values in the designer

    being able to place variables in an interface.



  • sebabas

    >Get in the habit of doing it. . and with VS2005's refactoring on the fly, their is no reason not to!

    Except that the compiler doesn't inline get and set quite often, so it is the cost of a function call.


  • Gsundi

    In properties you have the possability to execute code in the get and set blocks, a member variable could just be get or set.

    /Leyan


  • dhomburg

    Properties let you encapsulate the implementation details and check for validity of data. Especially in components you should use properties because Visual Studio's property browser displays properties and not fields. Read more about it on MSDN:

    http://msdn2.microsoft.com/en-us/library/65zdfbdt(VS.80).aspx


  • Axiverse

    The most important reason to use properties -

    you can't bind to fields or methods.

    Use properties!!!

    I guarantee it - if you don't, sometime in the future you will wish you did!

    Get in the habit of doing it. .  and with VS2005's refactoring on the fly, their is no reason not to!

     



  • SATISD9X

    Hello,

    I have noted an weird thing.

    I have a class 'C' that has private variable 'itemSet' and its public property 'ItemSet' with get and set.

    when I define a new object ' x ' of that class , of course I can arrive only the public property, but:

    when I debug the project:

    the object ' x ' encapsulates all of its items and properties, and also the private variable 'itemSet'.

    The weirder thing is that the values of the property and its private variable sometime differ.

    e.g:

    ItemSet is an arrayList of the following items:

    A,C,W,T initially

    and the get property code changes that order to be : A,C,T,W

    BUT 'itemSet' ,the private variable still :A,C,W,T . The code snippet follows:

    private ArrayList itemSet;

    //the property

    public ArrayList Itemset

    {

    get

    {

    if (((Item)prefix[0]).itemString != "")

    {

    this.itemSet.Clear();

    this.itemSet.InsertRange(0, this.prefix);

    this.itemSet.InsertRange(this.prefix.Count, this.suffix);

    }

    CompareClass compareType = new CompareClass(CompareField.Order);

    ArrayList orderedItemSet = OrderingItems(this.itemSet, compareType, true); //ascending order

    return orderedItemSet;

    }

    set

    {

    this.itemSet = value;

    }

    }

    How

    Thanks in advance for any help,

    Aya.


  • c.f.ong

    You can use Logic's behind setting is and check values, here is soms self-explaining code:


    public int MyInt
    {
    get
    {
    return _myInt;
    }
    set
    {
    // When the given value is negative or greater then 100, throw exception.
    if( value < 0 || value > 100 )
    {
    throw new ArgumentOutOfRangeException("value", "Can't be negative or greater then 100.");
    }

    // Set value.
    _myInt = value;

    // Raise Changed event.
    OnChanged();
    }
    }

    With properties you can also provide a layer of abstraction between the external interface and the internal implentation. You can change the implementation without modifying the interface,

    For example you can create a property that gives back the first element of an array, you can do this with a member:


    public string FirstElement
    {
    get
    {
    if( _elements.Lenght > 0 )
    {
    return _elements[0];
    }
    else
    {
    return null;
    }
    }
    }




  • public data members vs. properties