Sti*_*kly 8 c# enums operators unary-operator
我有以下内容:
MovingDirection.UP;
Run Code Online (Sandbox Code Playgroud)
我想用!运营商如下:
!MovingDirection.Up; // will give MovingDirection.Down
Run Code Online (Sandbox Code Playgroud)
(这是一个枚举)
我试过了:
public static MovingDirection operator !(MovingDirection f)
{
return MovingDirection.DOWN;
}
Run Code Online (Sandbox Code Playgroud)
...但我收到一个错误:
此一元运算符的参数类型必须是包含类型
有任何想法吗?
不,你不能在enums 上实现方法或运算符.您可以创建一个扩展方法:
public static MovingDirection Reverse(this MovingDirection direction)
{
// implement
}
Run Code Online (Sandbox Code Playgroud)
使用如下:
MovingDirection.Up.Reverse(); // will give MovingDirection.Down
Run Code Online (Sandbox Code Playgroud)
或者你可以使用enum-likeclass而不是realenum.
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |