2015-10-06 56 views
1

如何在enum中聲明°而不是度?c#enum描述度

//tilts declaration 
    public enum Tilts 
    { 
     mm = 0, 
     ° = 1, //degree 
     inch = 2 
    } 
+0

我不確定這是否可能,但我個人建議你不要!如果你想能夠以字符串形式輕鬆輸出它,我會考慮在枚舉中添加一個擴展方法。 – Octopoid

回答

0

要跟進我的意見,你應該改爲添加一個擴展方法將enum提供您所需要的格式的字符串:

public enum Tilts 
{ 
    Mm = 0, 
    Degree = 1, 
    Inch = 2 
} 

public static class TiltsExtensions 
{ 
    public static string ToSymbol(this Tilts tilts) 
    { 
     switch (tilts) 
     { 
      default: return tilts.ToString(); 
      case Tilts.Degree: return "°"; 
      // etc; 
     } 
    } 
} 

然後,每當你想輸出的符號形式,只是使用這樣的方法:

Console.WriteLine(tilts.ToSymbol()); 
+0

謝謝,我會試試! – user1562809