2011-07-19 78 views
6

我不喜歡下面↓是否有客戶枚舉類型和系統枚舉類型之間的區別

1)創建一個客戶枚舉(從DAYOFWEEK複製)

[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 

我真的不知道爲什麼系統枚舉類型是比客戶枚舉類型慢.... 有誰告訴我爲什麼? 謝謝...

UPDATE編輯: 添加[標記有ComVisible特性(真)]將枚舉。

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


    Monday = 1, 


    Tuesday = 2, 


    Wednesday = 3, 


    Thursday = 4, 


    Friday = 5, 


    Saturday = 6, 
} 

結果:

Tester : 5018ms 
DayOfWeek : 7032ms 

系統枚舉類型仍比客戶枚舉類型慢...

+0

在黑暗中只是一槍,但那枚(DayOfWeek)可能有特殊的文化處理。 – leppie

回答

1

枚舉可以使用[ComVisible(true)]或[Flags]進行修飾,每次它都會改變測試結果。

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

如果要比較蘋果蘋果應該添加[ComVisible(true)]屬性。