Click here to Skip to main content
13,248,562 members (84,464 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 8 Feb 2014

INotifyPropertyChanged Revisited with .NET Framework 4.5

, 8 Feb 2014
Rate this:
Please Sign up or sign in to vote.
INotifyPropertyChanged revisited with .NET Framework 4.5


Prior to this post, I have already discussed few ways of handling INotifyPropertyChanged interface. Now you might be thinking what's next? 

Well, today I'll talk about a much better and cleaner way of handling this interface, which is type-safe as well as less error prone. Till now, as a common practice, we were passing a property name as a parameter of our RaisedPropertyChanged method, which unfortunately has some demerits.

Let's say, what if user changed the name of the property and forgot to change the parameter passed inside RaisedPropertyChanged method. Now you might say that we can get rid of this by using Reflection. Even I agree with that, but again it comes with an additional associated cost. So, what's the solution now???

Enough of worries, this issue has been addressed in .NET 4.5, in which the developer can get rid of this parameter passing approach. Impressed?
An attribute titled CallerMemberName relieves us from all the worries because now this attribute will retrieve method and property name for us. Let's have a look at the code now.

Using the Code

public class ViewModelBase : INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged([CallerMemberName]string propertName="")
            var temp = PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs(propertName));

You will notice that the attribute is applied with the parameters supplied for RaisedPropertyChanged method. So, now the implementation of our property will be lot simpler as:

private string _employeeName;
       public string EmployeeName
           get { return _employeeName; }
               if (_employeeName != value)
                   _employeeName = value;

Now user is not forced to pass parameter to this RaisedPropertyChanged method. Hope you enjoyed learning this new feature.

Previous Post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

Questionother Properties Pin
Sami Abdelgadir10-Feb-14 0:59
memberSami Abdelgadir10-Feb-14 0:59 
AnswerRe: other Properties Pin
johannesnestler10-Feb-14 1:32
memberjohannesnestler10-Feb-14 1:32 
AnswerRe: other Properties Pin
Shweta Lodha10-Feb-14 8:26
memberShweta Lodha10-Feb-14 8:26 
QuestionRe: other Properties Pin
Member 98192705-Sep-14 6:35
memberMember 98192705-Sep-14 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 8 Feb 2014
Article Copyright 2014 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid