C # Some practical, easily forgotten features, experience and skills

Source: Internet
Author: User
Tags readline

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.