C# 中 pred 和 succ 的等效项是什么?
Pascal 是我的学习语言,我很好奇C#是否也有函数 pred
和 succ
。
这是我在 Pascal 中所做的,我想在 C# 中尝试
// in Pascal:
pred(3) = 2
succ(False) = True
pred('b') = 'a'
type enum = (foo, bar, baz);
succ(bar) = baz; pred(bar) = foo
相同的代码也适用于 C# 吗?如果是,这些函数的命名空间是什么?
(我搜索了谷歌,但找不到答案)
Pascal is my study language and I am curious whether C# also has the functions pred
and succ
.
This is what I have done in Pascal that I want to try in C#
// in Pascal:
pred(3) = 2
succ(False) = True
pred('b') = 'a'
type enum = (foo, bar, baz);
succ(bar) = baz; pred(bar) = foo
Is the same code applicable for C#, too? If so, what is the namespace for these functions?
(I searched Google, but couldn't find the answer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 中没有
pred
和succ
函数。您只需编写n - 1
或n + 1
即可。There aren't
pred
andsucc
functions in C#. You just writen - 1
orn + 1
.你在 C# 中有方法重载,所以很容易有 pred 和 succ,你可以通过以下方式做到这一点:
You have method overloading in c# so it's easy to have pred and succ, You can do it by:
使用扩展:
然后,这将起作用:
可能不被认为是非常惯用的,但仍然有效。必须覆盖其他整数类型(例如
short
)。如果您担心性能,请检查调用是否内联。注意:
++
和--
充当Inc
和Dec
,而不是Succ
和Pred。Using extensions:
Then, this would work:
Probably not considered very idiomatic, but still works. Would have to override for other integer types (like
short
). Check if the calls get inlined if you're concerned with performance.Note:
++
and--
act asInc
andDec
, notSucc
andPred
.您可以使用 ++ 或 -- 运算符:
不知道为什么您需要它,尽管您可以只执行 3+1 或 3-1 :)
You can use ++ or -- operator:
Not sure why you would need it though when you can just do 3+1 or 3-1 :)