Geeks With Blogs
Billy McCafferty whatever (but really just .NET)

It is a given that private members should be wrapped with public accessors for proper encapsulation of class data.  But it is common to see people publicly declaring events, such as with public event EventHandler SomeEventExposingHimself;.  Essentially, this is the same as exposing a private member to the world without accessors, such as with public string SomeStringThatYouShouldNotHaveAccessToDirectly;.  Any subscriber listening for the event is now tied directly to the actual class member.  C# makes it easy to provide accessors for your events, just as it does for class members, with the keywords add and remove. The following is an example of an event properly wrapped with public accessors:

public event EventHandler EventModestlyExposed {

add {

someEventNotWantingToExposeHimself += value;

}

remove {

someEventNotWantingToExposeHimself -= value;

}

}

 

private event EventHandler someEventNotWantingToExposeHimself;

Billy

Posted on Wednesday, August 9, 2006 8:14 AM C# | Back to top


Comments on this post: Wrapping C# Events with Accessors

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Public fields and public event fields are handled differently by the compiler. If you declare the following in a class.

public event EventHandler SomeEvent;

then look at the generated IL, you'll see that it expands it to:

public event EventHandler SomeEvent
{
add {someEvent += value;}
remove {someEvent -= value;}
}

So I would only add the extra code to wrap an event if I was doing something other than what the compiler is doing for me.
Left by Haacked on Aug 09, 2006 9:44 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Fair enough...so even if it was exposed directly, it would still be trivially simple to wrap it with public accessors down the road and include additional code within the add and/or remove without having to modify the subscriber(s).
Left by Billy on Aug 09, 2006 10:44 AM

# replica rolex GMT
Requesting Gravatar...
Vår website erbjuder också mest olik, och stilfullt spänna av kopian [url=http://www.partenairdesign.com]replica Omega[/url] för våra tittare.
Left by Nicole Thompsen on Sep 10, 2009 5:20 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
This is interesting !
Left by unibet on Nov 24, 2009 8:54 AM

# ww
Requesting Gravatar...
Great!wow gold Campus of the University who has clearly inherited the brain remnants of these shares of lonely trend! metin2 goldSome time ago, everyone net a man named Ringer's sillyX
Left by metin2 yang on Dec 11, 2009 3:18 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Cool stuff
Left by Simulation credit auto, prêt au on Dec 27, 2009 1:59 PM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Thanksss !
Left by bwin.fr on Jan 18, 2010 2:25 PM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Excellent post, thanks for sharing.
Left by unibet fr on Feb 07, 2010 10:14 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Whether you want to find trend shoes out for dating or leisure shoes for entertaining activities, Christian Louboutin shoes can satisfy you.
Left by Christian Louboutin Slingback on Apr 12, 2010 1:35 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
this post is very usefull thanks!
Left by jouer casino on Aug 30, 2010 9:10 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
it's very inteteresting i like it !
Left by france poker en ligne on Aug 30, 2010 11:30 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
it's very interesting!
Left by roulette en ligne on Oct 04, 2010 8:34 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
thanks for all!
Left by pronostic sportif on Oct 04, 2010 8:36 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
I am so grateful to read this such a wonderful post. Thank you for discussing this great topic. I will definitely bookmark your site. I really admire the writer for allotting their time for this impressive article. Surely many readers can benefit on this topic. Thank you.


Left by online degrees on Oct 11, 2010 12:19 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
I appreciate your idea here. Definitely it has a good content. Thank you for imparting more of your own thoughts. Good job!

Left by Mac Virtualization on Oct 11, 2010 3:30 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
the Wrapping C# Events with Accessors was very helpfull
Left by rachatcreditsurendettement on Nov 10, 2010 1:34 PM

# UGG Boots Sale
Requesting Gravatar...
Your article is extremely impressive. I never considered that it was feasible to accomplish something like that until after I looked over your post. You certainly gave a great perception on exactly how this whole process works. I will make sure to return for more advice. Thanks
Left by UGG Boots Sal on Dec 04, 2010 1:49 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
the blog is very interesting, I was impressed with your article, keep it up
Left by forex signals on Dec 28, 2010 7:11 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Great post, thanks for information.
Left by site de paris en ligne on Dec 28, 2010 10:00 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Very good post .I like it.Thank you.
Left by china wholesale electronics on Apr 06, 2011 7:08 AM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...

prom dresses 2011 from http://www.dresspromgirl.com
Left by prom dresses 2011 on Apr 21, 2011 9:26 PM

# re: Wrapping C# Events with Accessors
Requesting Gravatar...
Very nice an simple explanation. I simply used to declare everything publicly when they were required to be accessed around.

But now I will improve this habit.

What happens if we don't wrap anything and directly expose them as public ?
Left by Niraj Doshi on Sep 02, 2011 11:33 PM

Your comment:
 (will show your gravatar)


Copyright © Billy McCafferty | Powered by: GeeksWithBlogs.net