Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple meanings of the C# 'event' keyword?

Tags:

c#

events

keyword

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 the field attribute-target-specifier indicates that the attribute applies to the field; and the presence of the method attribute-target-specifier indicates that the attribute applies to the methods.

like image 618
Michael Edenfield Avatar asked Jul 12 '11 20:07

Michael Edenfield


People also ask

What is the meaning of the C?

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.

What words have 2 different meanings?

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).

What does C in text mean?

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.


1 Answers

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;
}
like image 159
Hans Passant Avatar answered Sep 28 '22 05:09

Hans Passant