I have a web service project and I found my team constantly needs to convert enum to a string. The only conversion is adding space. It wastes lots of time and involves in reflection which slows down the run time performance.
Enum type is one of the favorite types in F#. C#'s enum type does not have the ability to define a value with space. Having an enum value with space is very important because this feature can save me lots of time to output enum value as string.
For example, if the enum value can be "Post Release", the ToString function can output a nice string and won't have to use attribute and reflection to do the job. C# does have the ability to have space in enum but it will need to use reflect and emit to generate the type. If you can use F#, the problem can be easily solved. Please check the following F# code:
namespace ClassLibrary1
type public EnumEng =
Registration = 0
| ``Under Review``=1
| Approval = 2
| Release = 3
| ``Post Release`` = 4
type public EnumChn =
注册 = 0
| 审批 = 1
| 批准 = 2
| 发布 = 3
| 发布后 = 4
type public EnumIndex =
Reg = 0
| Review = 1
| Approval = 2
| Release = 3
| PostRelase = 4
module Test =
let a = EnumEng.``Under Review``
let b = EnumChn.审批
In the C# side, the intellisense won't display the enum value if it contains space. However, it will be displayed in debug mode. You can execute the following code and stop at the end of the function.
static void Main(string[] args)
{
//get all string from enum0
var strs = Enumerable.Range(0, 5)
.Select(n => (ClassLibrary1.EnumEng)n)
.Select(n => n.ToString())
.ToList();
// get all string from enum2
var i = Enumerable.Range(0, 5)
.Select(n => (ClassLibrary1.EnumChn)n)
.Select(n => n.ToString())
.ToList();
// parse string to enum
var v = strs.Select(n => Enum.Parse(n))
.ToList();
var x = ClassLibrary1.EnumIndex.PostRelase;
var str = (ClassLibrary1.EnumChn)x;
}
Both # and C# support non-English variable name, it will provide a way to localize the output as well. From the sample above, the EnumIndex is the type can used in C#/F# code. Once the value needs to be output as string, it can be convert to EnumEnglish (English string) or EnumChinese(Chinese string).
Is that convenient?
Tweet to fsharp
Enum type is one of the favorite types in F#. C#'s enum type does not have the ability to define a value with space. Having an enum value with space is very important because this feature can save me lots of time to output enum value as string.
For example, if the enum value can be "Post Release", the ToString function can output a nice string and won't have to use attribute and reflection to do the job. C# does have the ability to have space in enum but it will need to use reflect and emit to generate the type. If you can use F#, the problem can be easily solved. Please check the following F# code:
namespace ClassLibrary1
type public EnumEng =
Registration = 0
| ``Under Review``=1
| Approval = 2
| Release = 3
| ``Post Release`` = 4
type public EnumChn =
注册 = 0
| 审批 = 1
| 批准 = 2
| 发布 = 3
| 发布后 = 4
type public EnumIndex =
Reg = 0
| Review = 1
| Approval = 2
| Release = 3
| PostRelase = 4
module Test =
let a = EnumEng.``Under Review``
let b = EnumChn.审批
In the C# side, the intellisense won't display the enum value if it contains space. However, it will be displayed in debug mode. You can execute the following code and stop at the end of the function.
static void Main(string[] args)
{
//get all string from enum0
var strs = Enumerable.Range(0, 5)
.Select(n => (ClassLibrary1.EnumEng)n)
.Select(n => n.ToString())
.ToList();
// get all string from enum2
var i = Enumerable.Range(0, 5)
.Select(n => (ClassLibrary1.EnumChn)n)
.Select(n => n.ToString())
.ToList();
// parse string to enum
var v = strs.Select(n => Enum.Parse
.ToList();
var x = ClassLibrary1.EnumIndex.PostRelase;
var str = (ClassLibrary1.EnumChn)x;
}
Both # and C# support non-English variable name, it will provide a way to localize the output as well. From the sample above, the EnumIndex is the type can used in C#/F# code. Once the value needs to be output as string, it can be convert to EnumEnglish (English string) or EnumChinese(Chinese string).
Is that convenient?
Tweet to fsharp