Suddenly thought, want to get the value of the enumeration by name, read a lot of data, found above all just explain the syntax of the enumeration, for its practical application, do not mention, can only switch to judge? Not good, this is too much dirt. Later discovered that the enumeration also has such usage, really uses, looks at the code:
Code
namespace EnumTest
{
enum date { sun, mon, tue, wes, thu, fri, sat }
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入星期的名称:");
string name = Console.ReadLine();
//Type参数表示要转换成的枚举的类型,true指示忽略大小写
object obj = Enum.Parse(typeof(date), name, true);
Console.WriteLine("输出星期的数字:");
Console.WriteLine(obj + ":" + (int)obj);
}
}
}
Let's take a look at the results of the operation:
How, is not feel very practical ah.
The next thing to introduce is commissioned (delegate), there are people who do not know much about delegate can see what I have written before an essay what is a Delegate (delegate). We usually use delegates as a single method of specifying delegates, but what if we need to specify by parameter dynamics? Is it also a switch? This is too much trouble, there is a better way to look at the code:
Code
namespace DelegateTest
{
class Person
{
public void FirstMethod()
{
Console.WriteLine("这是第一个方法!");
}
public void SecondMethod()
{
Console.WriteLine("这是第二个方法!");
}
}
delegate void dele();
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入方法的名字:");
string name = Console.ReadLine();
Person p = new Person();
//Type参数是要转换的委托的类型,p是要调用的委托的实例,true指示忽略大小写
dele d = Delegate.CreateDelegate(typeof(dele), p, name + "Method", true) as dele;
d.Invoke();
Console.WriteLine("输入方法的名字:");
name = Console.ReadLine();
d = Delegate.CreateDelegate(typeof(dele), p, name + "method", true) as dele;
d.Invoke();
}
}
}
The following is the result diagram of the operation: