Is it possible to create a new statement in C #?

I know that you can overload an existing statement. I want to know if a new statement can be created. Here is my script.

I want this:

var x = (y < z) ? y : z; 

To be equivalent to this:

 var x = y <? z; 

In other words, I would like to create my own operator <? .

+54
operators c #
Jun 24 '09 at 18:32
source share
7 answers

No, It is Immpossible. You will need to create a method instead

+35
Jun 24 '09 at 18:33
source share

No, but you can overload some existing statements in C #.

In some other languages, such as F #, you can use:

 let (<?) = min 
+27
Jun 24 '09 at 18:35
source share

As already mentioned in other answers, you cannot create a new statement - at least without changing the lexer and parser built into the compiler. Basically, the compiler is built to recognize that a single character, such as < or ? , or a pair like >> or <= is an operator and specifically processes it; he knows that i<5 is an expression, not a variable name, for example. Recognizing the operator as an operator is a separate process that allows you to decide what the operator actually does and is much more closely integrated into the compiler - thatโ€™s why you can configure the latter, but not the first.

For languages โ€‹โ€‹that have an open source compiler (such as GCC), you can theoretically modify the compiler to recognize a new statement. But it would not be so simple, and in addition, everyone will need your own compiler to use your code.

+17
Jun 24 '09 at 18:47
source share

Not only can you not do this, but why do you want to?

I'm not sure what types are y and z, but if they have a numeric value type, you can probably use:

 var x = Math.Min(y, z); 

Although personally, I would prefer:

 var x = (y < z) ? y : z; 

But am I a little ?: junky.

Good code is not only dense and efficient, but also readable. Even if you are the only one who has ever read this, will you return to this operator <? one day, and wonder what the hell it is.

+5
Jun 24 '09 at 19:10
source share

No, but instead you can create extension methods instead

 y.MethodName(z) 
+3
Jun 24 '09 at 19:13
source share

I am surprised that no one mentioned "order of operations" .

When the compiler evaluates an expression, it must deal with the operations in the correct order, so that (1+2*3) = (2*3+1) multiplication always happens before adding to the same "level" in the expression,

When you redefine and execute a statement, you can change what the statement does, but not the order in which the compiler evaluates it. If you created a new statement, it is impossible to tell the compiler what order to evaluate it in relation to others. So if you write x <? 2 + 5 x <? 2 + 5 x <? 2 + 5 x <? 2 + 5 Do you x <? 2 x <? 2 x <? 2 x <? 2 add 5 first or add first and then do x <? 7 x <? 7 x <? 7 x <? 7 .

0
Jul 26 '18 at 20:34
source share

can you try to overload another operator, for example % or + , to act as the operator <? .

It will be fun

-four
Mar 31 '14 at 16:25
source share



All Articles