views:

112

answers:

3

Hi All

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
}
+2  A: 

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.

driis
True, but you need to let him know that if he *did* implement a custom preprocessor, he's exposing himself to potential physical violence in the workplace :)
Dave Markle
lol +1 for both
lucifer
+6  A: 

No.

The closest you could get would be an extension method:

Control.WasClicked()
Henk Holterman
Yeah, I love extensino methods. But I already know how to use them. +1 though :)
lucifer
+4  A: 

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.

Daniel Chambers