C# 是否可以通过位置值像数组一样遍历枚举?

C# 是否可以通过位置值像数组一样遍历枚举?,c#,enums,C#,Enums,是否有一种方法可以遍历枚举或检索其在枚举列表中的位置。我有下面的示例代码 private static void Main(string[] args) { DateTime sinceDateTime; Counters counter = new Counters(); // Iterate through time periods foreach (TimePeriodsToTest testTimePeri

是否有一种方法可以遍历枚举或检索其在枚举列表中的位置。我有下面的示例代码

    private static void Main(string[] args)
    {
        DateTime sinceDateTime;
        Counters counter = new Counters();

        // Iterate through time periods
        foreach (TimePeriodsToTest testTimePeriod in Enum.GetValues(typeof(TimePeriodsToTest)))
        {
            // e.g. DateTime lastYear = DateTime.Now.AddDays(-365);
            sinceDateTime = DateTime.Now.AddDays((double)testTimePeriod);
            var fileCount =
                Directory.EnumerateFiles("c:\\Temp\\")
                    .Count(path => File.GetCreationTime(path).Date > sinceDateTime);

            Console.WriteLine("Files since " + -(double)testTimePeriod + " days ago is : " + fileCount);
            // counter.TimePeriodCount[testTimePeriod] = fileCount;
        }
    }

    public enum TimePeriodsToTest
    {
        LastDay = -1,
        LastWeek = -7,
        LastMonth = -28,
        LastYear = -365
    }

    public class Counters
    {
        public int[] TimePeriodCount = new int[4];
    }

    public class Counters2
    {
        public int LastDay;
        public int LastWeek;
        public int LastMonth;
        public int LastYear;
    }
因此,我想将值
fileCount
存储到
counter.TimePeriodCount[]
中。如果我能得到
testTimePeriod
的“位置值”,那么它将很好地插入数组
counter.TimePeriodCount[]
。但我还没有找到如何做到这一点

如果最后一天、最后一周等是1、2、3、4,那么这不是问题,但它们不是,我有问题

或者,是否有一种方法可以在后续迭代中将
文件计数
存储到
Counters2.LastDay
Counters2.LastWeek
等中

还是我只是走错路了

更新 “KuramaYoko”给出的建议可以通过在解决方案中添加字典来实现,但我发现Jones6给出的解决方案更加优雅,因为它不需要添加字典。感谢您的时间和努力,因为我从这两个答案中学到了一些东西:-)

Update2现在我了解了AlexD解决方案的使用方法,这也是解决问题的一个很好的方法。谢谢。

您可以使用方法获取所有枚举值。我怀疑订单是否有保证,因此您可能需要对值进行排序

int[] values = Enum.GetValues(typeof(TimePeriodsToTest))
    .Cast<int>()
    .OrderBy(x => x)
    .ToArray();

for (int k = 0; k < values.Length; k++)
{
    sinceDateTime = DateTime.Now.AddDays(values[k]);
    fileCount = ....
    counter.TimePeriodCount[k] = fileCount;
}
int[]values=Enum.GetValues(typeof(TimePeriodsToTest))
.Cast()
.OrderBy(x=>x)
.ToArray();
for(int k=0;k
顺便说一句,同样地,我们会给您提供名称。

您可以使用方法获取所有枚举值。我怀疑订单是否有保证,因此您可能需要对值进行排序

int[] values = Enum.GetValues(typeof(TimePeriodsToTest))
    .Cast<int>()
    .OrderBy(x => x)
    .ToArray();

for (int k = 0; k < values.Length; k++)
{
    sinceDateTime = DateTime.Now.AddDays(values[k]);
    fileCount = ....
    counter.TimePeriodCount[k] = fileCount;
}
int[]values=Enum.GetValues(typeof(TimePeriodsToTest))
.Cast()
.OrderBy(x=>x)
.ToArray();
for(int k=0;k

顺便说一句,同样的,我会给你名字。

你应该能够做到这一点

foreach (var testTimePeriod in Enum.GetValues(typeof(TimePeriodsToTest)).Cast<TimePeriodsToTest>().Select((x, i) => new { Period = x, Index = i}))
{
     counter.TimePeriodCount[testTimePeriod.Index] = fileCount;
}
foreach(Enum.GetValues中的var testTimePeriod(typeof(TimePeriodsToTest)).Cast().Select((x,i)=>new{Period=x,Index=i}))
{
counter.TimePeriodCount[testTimePeriod.Index]=fileCount;
}

您应该能够做到这一点

foreach (var testTimePeriod in Enum.GetValues(typeof(TimePeriodsToTest)).Cast<TimePeriodsToTest>().Select((x, i) => new { Period = x, Index = i}))
{
     counter.TimePeriodCount[testTimePeriod.Index] = fileCount;
}
foreach(Enum.GetValues中的var testTimePeriod(typeof(TimePeriodsToTest)).Cast().Select((x,i)=>new{Period=x,Index=i}))
{
counter.TimePeriodCount[testTimePeriod.Index]=fileCount;
}

感谢您的建议,可能会重复。我能够做到这一点,但我必须建立一个字典来解决这个问题。琼斯的回答更短,为我更优雅地解决了这个问题。但是谢谢你的建议,因为我从来没有想过用字典作为可能的解决办法。我能够做到这一点,但我必须建立一个字典来解决这个问题。琼斯的回答更短,为我更优雅地解决了这个问题。但是谢谢你的建议,因为我从来没有想过使用字典作为可能的解决方案。他想要枚举项的索引值,而不是存储在项中的实际值。enum.GetValues(typeof(TimePeriodsToTest))只会得到值(-1,-7,-28,-365)。当我定义枚举时,我试图遍历它们,但仍然拥有每个特定枚举值的0、1、2、3位置值。“对于我来说,最优雅的解决方案是琼斯提出的。”艾伦只是想确定一下。我更新了帖子。这就是你的意思吗?@AlexD啊。。。我没有意识到您的解决方案意味着不在循环中使用枚举类型,而是将枚举值强制转换到一个数组中,然后遍历该数组。我的想法仍然停留在使用循环中的枚举值上。感谢您提供的替代解决方案。@Alan或者,您可以将
TimePeriodCount
设置为
List
而不是数组,只需在每个步骤上添加一个新项。他想要的是枚举项的索引值,而不是存储在该项中的实际值。enum.GetValues(typeof(TimePeriodsToTest))只会获取值(-1, -7, -28, -365)。当我定义枚举时,我试图遍历它们,但仍然有每个特定枚举值的0、1、2、3位置值。最优雅的解决方案是jones6给出的。@Alan只是为了确定。我更新了帖子。这就是你的意思吗?@AlexD Ah…我不知道你的解决方案意味着不使用枚举类型在循环中,但将枚举值强制转换为数组,然后在该数组中迭代。我的想法仍然停留在在在循环中使用枚举值上。感谢您提供的替代解决方案。@或者,您可以将
TimePeriodCount
a
List
替换为数组,只需在每个数组中添加一个新项即可谢谢。这对我帮助最大。我只需要做少量的代码更改。我每天都学习新东西。谢谢。谢谢。这对我帮助最大。我只需要做少量的代码更改。我每天都学习新东西。谢谢。