C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public enum BookType
{
Fantasy = 1,
History = 2,
Child = 3,
}
class Program
{
static void Main(string[] args)
{
try
{
BookType type = (BookType) Enum.Parse(typeof(BookType), "History");
// type=2 (History)
}
catch (Exception e)
{
// Error! Not found.
Console.WriteLine(e);
}
try
{
BookType type = (BookType) Enum.Parse(typeof(BookType), "CHILD", true /*igonre Case*/);
// type=3 (Child)
}
catch (Exception e)
{
// Error! Not found.
Console.WriteLine(e);
}
}
}
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public enum BookType
{
Fantasy = 1,
History = 2,
Child = 3,
}
class Program
{
static void Main(string[] args)
{
BookType type;
if (Enum.TryParse("History", out type))
{
// type=2 (History)
Console.WriteLine(type);
}
if (Enum.TryParse("CHILD", true /*igonre Case*/, out type))
{
// type=3 (Child)
Console.WriteLine(type);
}
}
}
}