Is it possible to create a new operator in c#?

后端 未结 7 1759
南旧
南旧 2020-11-29 06:46

I know you can overload an existing operator. I want to know if it is possible to create a new operator. Here\'s my scenario.

I want this:

var x =          


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 07:39

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

    I'm not sure what type your y and z are, but if they are of a numeric value type, you could probably use:

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

    Though personally, I would still prefer:

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

    But I'm a bit of a ? : junky.

    Good code is not just tight and efficient, but also readable. Even if you are the only one ever reading it, you'd come back to that operator one day and wonder what the heck that did.

提交回复
热议问题