I was recently re-reading some old posts on Eric Lippert's ridiculously awesome blog and came across this tidbit:
A considerable fraction of the keywords of C# are used in two or more ways: fixed, into, partial, out, in, new, delegate, where, using, class, struct, true, false, base, this, event, return and void all have at least two different meanings.
Just for fun my coworkers and I quizzed ourselves and I was able to come up with at least two uses for all but one of those keywords. The one that stumped me is event
.
Obviously, using event
when declaring a member field of a delegate type turns it into an event (e.g. only add/remove operators are exposed). What's the other meaning of event?
EDIT (Answer):
Thanks to @Hans Passant I dug up this bit out of the C# spec that explains the other use of event -- as (the default) attribute target specifier for attributes on an event (from section 17.2):
An attribute specified on an event declaration that omits event accessors can apply to the event being declared, to the associated field (if the event is not abstract), or to the associated add and remove methods. In the absence of an attribute-target-specifier, the attribute applies to the event. The presence of the
event
attribute-target-specifier indicates that the attribute applies to the event; the presence of thefield
attribute-target-specifier indicates that the attribute applies to the field; and the presence of themethod
attribute-target-specifier indicates that the attribute applies to the methods.
Century, sometimes abbreviated as c. or C., a period of 100 years. Cent (currency), abbreviated c. or ¢, a monetary unit that equals 1⁄100 of the basic unit of many currencies. Caius or Gaius, abbreviated as C., a common Latin praenomen. Circa, abbreviated as c.
Homonyms, or multiple-meaning words, are words that have the same spelling and usually sound alike, but have different meanings (e.g. dog bark, tree bark).
Hopefully, it's pretty easy to c why c can stand for "see." In addition to being used on its own, c is used to mean "see" in other chat and text acronyms, such as IC and cya. While s can also stand for "see," such as in the acronyms SYS and IYSWIM, s never means "see" when used on its own.
As the attribute target specifier. I can't think of a good reason you would do this:
[AttributeUsage(AttributeTargets.Event)]
class MyAttribute : Attribute { }
class foo {
[event: MyAttribute]
public event EventHandler goo;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With