2015-10-06 54 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