anon271334
anon271334

Reputation:

Keyword Implementation in C#

Is there a way, and how would I go about implementing my own keyword such as in, and as (etc), to be used in my code?

Here is what I had in mind. I want to (just for my own personal reasons, I guess) add a few keywords of my own, one of which would be the "was" keyword:

if(Control was Clicked)
{
   // etc etc
}

Upvotes: 1

Views: 264

Answers (3)

Daniel Chambers
Daniel Chambers

Reputation: 1665

No, you can't add to the C# language, short of writing your own compiler.

However, your "was" keyword makes me think you might be looking for a way to declaratively handle events. Microsoft have a library called "Reactive Extensions for .NET" (Rx) that is an extension to LINQ that allows you to deal with events in a declarative fashion.

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273591

No.

The closest you could get would be an extension method:

Control.WasClicked()

Upvotes: 5

driis
driis

Reputation: 164341

No, you cannot add keywords to C# - at least not without writing a compiler for yourself. If what you want to do is simple, however, perhaps you could do it using a custom preprocessor. You would lose some syntax highlighting and error checking in Visual Studio, though.

One language for the CLR, that is designed to be extensible like that, is Boo.

Upvotes: 1

Related Questions