我可以在 C# 中创建三元运算符吗?
我想为 < 创建一个三元运算符b< c 是 a <乙&& b< c.或您能想到的任何其他选项 < b> c等等...我是我自己的短片的粉丝,自从我在高中学习编程以来我就想创建它。
如何?
I want to create a ternary operator for a < b < c which is a < b && b < c. or any other option you can think of that a < b > c and so on... I am a fan of my own shortform and I have wanted to create that since I learned programming in high school.
How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要相信那些讨厌的人;)
您可以在 C# 中做到这一点。这是一个示例实现 - 我基于 Icon 的方式进行链接...如果比较成功,结果是正确的参数,否则返回特殊的“失败”结果。
您需要使用的唯一额外语法是在第一项之后调用
Chain()
。Don't believe the haters ;)
You can do this in C#. Here's an example implementation — I based the chaining off the way that Icon does theirs... if a comparison succeeds, the result is that of the right parameter, otherwise a special "failed" result is returned.
The only extra syntax you need to use is the call to
Chain()
after the first item.抱歉,您无法在 C# 中创建自己的运算符。
您可以使用扩展方法来启用流畅的语法,例如
或者,如果您非常聪明,您可以这样做:
这样做很棘手,但可能。 (提示:定义 IsLessThan 返回的自定义对象,该对象跟踪其边界并了解它如何与该对象的其他实例组合。本质上,这就是 LINQ-to-SQL 结合Where、Select 等的工作方式。)
但您无法在 C# 中定义自己的运算符语法。
如果您对可以定义自己的运算符的语言感兴趣,您可以考虑研究 F#。
Sorry, you cannot create your own operators in C#.
You could use extension methods to enable a fluent syntax like
Or, if you were being extremely clever, you could do:
doing so is tricky, but possible. (Hint: define a custom object that IsLessThan returns that tracks its bounds and understands how it is combined with other instances of the object. Essentially this is how LINQ-to-SQL works with regard to combining Where, Select, and so on.)
But you cannot define your own operator syntaxes in C#.
If you are interested in languages where you can define your own operators, you might consider looking into F#.
你不能那样做。您只能实现现有的运算符,并且没有三元 .<.<。 C# 中的运算符。
此外,这样的运算符与现有的比较运算符会产生歧义。例如,表达式
a == b == c == d
是否意味着((a == b) == c) == d
或( a == b == c) == d
?You can't do that. You can only implement existing operators, and there is no ternary .<.<. operator in C#.
Besides, such an operator would be ambiguous with existing comparing operators. For example, would the expression
a == b == c == d
mean((a == b) == c) == d
or(a == b == c) == d
?您是否尝试过在谷歌中搜索三元运算符以查看是否已经存在某些内容?
Have you tried searching for ternary operators in google to see if something already exists?
不,C# 中没有。这种方式的语言扩展在任何当前版本的 C# 中都不可用,但 Luca Bolognese 指出使用编译器作为服务和一些功能,可以允许以这种方式扩展语言:http://microsoftpdc.com/Sessions/FT11。
No. Not in C#. Language extension in this way is not available in any current version of C#, but Luca Bolognese pointed to using the compiler as a service and some functionality which could permit extending the language in such a way here: http://microsoftpdc.com/Sessions/FT11.
如果您想对原始数据类型执行此操作,那么您就不走运了。 C# 不支持向其中添加运算符。
在您自己的数据类型中,可以返回存储中间结果和比较结果的特殊数据类型。但是,我建议您坚持使用 c# 语言 - 如果您确实想要
a
a
a
a
a
a
a
a
a < b< c
运算符风格,切换到 Python。If you want to do that for the primitive datatypes, you're out of luck. C# doesn't support adding operators to those.
In your own datatypes it is possible to return a special datatype which stores the intermediate result and the comparisson result. However, I suggest you just stick to the c# language - if you really want the
a < b < c
operator style, switch to Python.