Simplified Event Raising Pattern in C#

When creating events most people do something like this

        public event PropertyChangedEventHandler PropertyChanged; 

        internal void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

A much simpler approach is to instead do this


        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        internal void RaisePropertyChanged(string propertyName)
        {
            // No need for null check because of empty delegate
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

which eleminates the need for the null check and eliminates the potential race conditions in multi-threaded code.

Tags: , ,

6 Responses to “Simplified Event Raising Pattern in C#”

  1. KURT says:


    Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. No prescription drugs. Order drugs online

    Buy:Super Active ED Pack.Cialis Super Active+.Viagra Soft Tabs.Cialis Professional.Cialis Soft Tabs.Propecia.Maxaman.Levitra.Viagra.Viagra Professional.Viagra Super Active+.Soma.VPXL.Zithromax.Tramadol.Viagra Super Force.Cialis….

  2. DARREN says:


    Medicamentspot.com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. Online Pharmacy. Buy drugs online

    Buy:Zocor.Cozaar.Lipitor.Prozac.Advair.Ventolin.Zetia.Seroquel.Amoxicillin.Lasix.Female Cialis.SleepWell.Nymphomax.Female Pink Viagra.Benicar.Aricept.Buspar.Wellbutrin SR.Acomplia.Lipothin….

  3. ALEJANDRO says:


    Medicamentspot.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. Low price drugs. Buy pills online

    Buy:Amoxicillin.Ventolin.Lipitor.Prozac.Buspar.Wellbutrin SR.Cozaar.Seroquel.Lipothin.SleepWell.Zetia.Benicar.Female Cialis.Female Pink Viagra.Nymphomax.Lasix.Zocor.Aricept.Acomplia.Advair….

  4. IAN says:


    CheapTabletsOnline.com. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. Low price pills. Buy pills online

    Buy:Accutane.Human Growth Hormone.Retin-A.Valtrex.100% Pure Okinawan Coral Calcium.Arimidex.Actos.Mega Hoodia.Prednisolone.Petcam (Metacam) Oral Suspension.Lumigan.Zyban.Prevacid.Zovirax.Synthroid.Nexium….

  5. TOMMY says:


    CheapTabletsOnline.com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. No prescription pills. Order pills online

    Buy:Super Active ED Pack.Tramadol.Propecia.Viagra Super Force.Viagra Soft Tabs.VPXL.Viagra.Viagra Super Active+.Cialis.Levitra.Cialis Super Active+.Zithromax.Soma.Cialis Soft Tabs.Viagra Professional.Cialis Professional.Maxaman….

  6. LYNN says:


    NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

Leave a Reply