gpt4 book ai didi

c# - 客户枚举类型和系统枚举类型之间有什么区别吗

转载 作者:太空狗 更新时间:2023-10-29 21:55:30 28 4
gpt4 key购买 nike

我做了如下测试↓

1) 创建一个客户枚举(从星期几复制)

[Serializable]
public enum Tester
{
// 概要:
// Indicates Sunday.
Sunday = 0,
//
// 概要:
// Indicates Monday.
Monday = 1,
//
// 概要:
// Indicates Tuesday.
Tuesday = 2,
//
// 概要:
// Indicates Wednesday.
Wednesday = 3,
//
// 概要:
// Indicates Thursday.
Thursday = 4,
//
// 概要:
// Indicates Friday.
Friday = 5,
//
// 概要:
// Indicates Saturday.
Saturday = 6,
}

2) 创建两个测试方法...

    static void TestEnumToString()
{
var t = Tester.Sunday;
Enumerable.Range(0, 1000000).ToList().ForEach(i => t.ToString());
}

static void DayOfWeekEnumToString()
{
var t = DayOfWeek.Sunday;
Enumerable.Range(0, 1000000).ToList().ForEach(i => t.ToString());
}

3)主要方法

    static void Main()
{
Stopwatch sw = new Stopwatch();
sw.Start();
TestEnumToString();
sw.Stop();
Console.WriteLine("Tester:" + sw.ElapsedMilliseconds);


sw = new Stopwatch();
sw.Start();
DayOfWeekEnumToString();
sw.Stop();
Console.WriteLine("DayOfWeek:" + sw.ElapsedMilliseconds);

Console.ReadKey();
}

4) 结果:

Tester : 3164ms
DayOfWeek : 7032ms

我真的不知道为什么系统类型枚举比客户枚举类型慢....谁能告诉我为什么?谢谢...

更新编辑:将 [ComVisible(true)] 添加到枚举中。

[ComVisible(true)]
[Serializable]
public enum Tester
{
Sunday = 0,


Monday = 1,


Tuesday = 2,


Wednesday = 3,


Thursday = 4,


Friday = 5,


Saturday = 6,
}

结果:

Tester : 5018ms
DayOfWeek : 7032ms

系统枚举类型仍然比客户枚举类型慢...

最佳答案

枚举可以用 [ComVisible(true)] 或 [Flags] 装饰,每次它都会改变你的测试结果。

[Serializable]
//[Flags]
[ComVisible(true)]
public enum Tester
{
// 概要:
// Indicates Sunday.
Sunday = 0,

关于c# - 客户枚举类型和系统枚举类型之间有什么区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6742397/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com