When do I need to use automatic poperties and when properties with propertychanged event ?

Posted by Honey on Stack Overflow See other posts from Stack Overflow or by Honey
Published on 2010-05-29T09:53:06Z Indexed on 2010/05/29 10:02 UTC
Read the original article Hit count: 371

Filed under:
|
|
|

Hello,

I am using wpf and its C sharp!

I have this in my Animal.cs clas

private string _animalName;

    public string AnimalName
    {
        get { return _animalName; }
        set
        {
            if(_animalName!= value)
            {
                _animalName= value;
                this.NotifyPropertyChanged("AnimalName");
            }
        }
    }

I could also write:

public string AnimalName {get;set;}

There is no difference in binding and validation. Everythings works as before when I exchange the code.

Is this due to the fact that I only create new animals but I do not allow to update the animals name in my application ?

So I need to call the propertyChanged("AnimalName"); only when I want to change its property value?

I am a c# beginner ;)

© Stack Overflow or respective owner

Related posts about c#

Related posts about property