在 C# 中使用枚举进行数学运算(例如 DayOfWeek)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/951924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math with Enums (e.g. DayOfWeek) in C#
提问by nicolaskruchten
Why is it that the following code won't work:
为什么以下代码不起作用:
endDate.AddDays(7-endDate.DayOfWeek);
While this will:
虽然这将:
endDate.AddDays(0-endDate.DayOfWeek + 7);
?
?
(By "won't work" I mean results in the following compilation error: "cannot convert from 'System.DayOfWeek' to 'double'")
(“行不通”是指导致以下编译错误:“无法从‘System.DayOfWeek’转换为‘double’”)
采纳答案by Erik Funkenbusch
To expand upon what Lasse said (or rather, make it a little more explicit).
扩展 Lasse 所说的内容(或者更确切地说,使其更明确一些)。
Because 0 is convertable to an Enum type,
因为 0 可以转换为 Enum 类型,
0 - endDate.DayOfWeek becomes
(DayOfWeek)0 - endDate.DayOfWeek
And since you can subtract one enum from another and get an integer difference:
因为你可以从另一个枚举中减去一个枚举并得到一个整数差:
(DayOfWeek)0 - endDate.DayOfWeek == (int)endDate.DayOfWeek
Thus, since the result of the subtraction is an int, you can then add 7 to it.
因此,由于减法的结果是一个 int,因此您可以将 7 添加到它。
endDate.AddDays(0-endDate.DayOfWeek + 7);
So, if Monday's Enum value is 1
所以,如果星期一的 Enum 值为 1
0 - endDate.DayOfWeek == -1 + 7 == 6
However, you can't do the reverse.
但是,您不能反过来。
endDate.DayOfWeek - 0 + 7,
because the result type of the calculation is dependant upon the leftmost side. Thus, while 0 - endDate.DayOfWeek results in an integer, endDate.DayOfWeek - 0 results in an enum DayOfWeek.
因为计算的结果类型取决于最左侧。因此,虽然 0 - endDate.DayOfWeek 产生一个整数,但 endDate.DayOfWeek - 0 产生一个枚举 DayOfWeek。
Most interestingly, you could use this side-effect to get the value of an enum without casting, though I would consider this hackish and confusing... thus to be avoided.
最有趣的是,您可以使用这种副作用来获取枚举的值而不进行强制转换,尽管我会认为这很骇人听闻……因此可以避免。
int enumValue = -(0 - endDate.DayOfWeek);
回答by John Fisher
This is very interesting. The right way to do this is:
这是非常有趣的。正确的做法是:
endDate.AddDays(7 - (int)endDate.DayOfWeek);
But, your question isn't about a solution, but a reason for the behavior. It has something to do with the way the compiler treats a zero. Either line fails if no zero is present, while both lines work if a zero is present.
但是,您的问题不是关于解决方案,而是关于行为的原因。它与编译器处理零的方式有关。如果不存在零,则任一行都失败,而如果存在零,则两行都有效。
回答by Lasse V. Karlsen
You can subtract two enum values to get their integer value difference:
您可以减去两个枚举值以获得它们的整数值差异:
using System;
namespace ConsoleApplication10
{
public enum X { A, B, C, D }
public class Program
{
static void Main()
{
var x = X.D + X.A;
Console.Out.WriteLine(x);
Console.In.ReadLine();
}
}
}
Will print out 3.
将打印出 3。
But you can't add, probably makes no sense.
但是你不能添加,可能没有意义。
In the case of "0", 0 is auto-convertible to all enum types, so basically "0 - enumvalue" means the same as "(enumtype)0 - enumvalue", which again works.
在“0”的情况下,0 可自动转换为所有枚举类型,因此基本上“0 - enumvalue”的含义与“(enumtype)0 - enumvalue”相同,后者再次起作用。