Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redefine the names for the standard keywords in C#

I have the interesting idea. I want to redefine the keywords in C#, like replace the if keyword to the MyIf or something else. Do someone have any idea about how to do it?

As I think it have to look something like this:

namespace
{
   #define MyIf = if;
   #define MyElse = else;
   ...

   public someclass
   {
        public void someMethod()
        {
            MyIf(true)
            {
               ...
            }
            MyElse
            {
               ...
            }
        }
   }
}

Added:

Maybe there is the way how to make the C++ or C library which will redefine the part of standard core of C#?

**Notice. I know that it is the bad programming practice and I ask all the programmers to not use the answer in your enterprise code. **

like image 531
Maris Avatar asked Mar 04 '13 08:03

Maris


2 Answers

That's not possible -- C# does not have a preprocessor like C and C++ do, so there is no mechanism to transform the source before the compiler sees it. And of course the compiler will only recognize the standard if, so there is also no other option that somehow transforming the source before the compiler sees it.

Even if it were possible, it would be just a step below Cthulhu in the horror scale.

like image 148
Jon Avatar answered Sep 28 '22 17:09

Jon


In c# you can override the overloadable operators (+ , - , ==) more info here: http://msdn.microsoft.com/en-us/library/8edha89s(v=vs.80).aspx

But you can't really override conditional operators.

The same goes for these operators =, ., ?:, ->, new, is, sizeof, typeof, they cannot be overloaded.

C# does not support macros, but of course you could write a custom parser that handles macros before eventually sending your code to the c# compiler, take a look at Microsoft Roslyn for that.

http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx

like image 31
animaonline Avatar answered Sep 28 '22 16:09

animaonline