关于重构之Switch的处理【二】

前面写了一个Switch的处理的例子,

public class Fun{

        public void IFNumberid(int flag)  

        {

                switch (flag)
                        {
                            case 1:
                                Console.WriteLine("优秀");
                                break;
                            case 2:
                                 Console.WriteLine("良好");
                                  break;
                            case 3:
                                  Console.WriteLine("一般");
                                  break;
                            case 4:
                                  Console.WriteLine("不及格");
                                  break;

                default:

                                  Console.WriteLine("");
                                  break;
                      }
  

        }  

    以上Case里面是一个有序的数字,处理方法见 switch中case如果是有序的话,如何处理

那么假如我们换成了以下的事例呢?

 

public class Fun{

        public void NameResult(String flag)  

        {

                switch (flag)
                        {
                            case "a":
                                Console.WriteLine("优秀");
                                break;
                            case "张三":
                                 Console.WriteLine("良好");
                                  break;
                            case "c":
                                  Console.WriteLine("一般");
                                  break;
                            case "g":
                                  Console.WriteLine("不及格");
                                  break;

                default:

                                  Console.WriteLine("");
                                  break;
                      }
  

        }  

  

这里的Case并不是一个有序的,那么我们利用数组索引的办法将并不可行,此时,则可以考虑使用Dictionary<Tkey, Tvalue>来实现Switch中Case的替换

 

public class Fun{

        public void NameResult(String flag)  

        {

          Dictionary<string, string> cl = new Dictionary<string, string>();

                       cl.Add("a","不及格");

                       cl.Add("张三","良好");

                       cl.Add("c","一般");

                      cl.Add("g","不及格");
         foreach (KeyValuePair<string, string> a in cl)
                  {
              if(a.key==flag)

              {

                 Console.WriteLine(a.Value);

                 return ;                                          

              }

                  }
         Console.WriteLine("");

        }

}

另外,很多时候,我们可能根据某个值直接去执行某个方法,这时,采用以上方法也是可行的,

  先声明一个委托

    private delegate void EatDelegate();

     private  void functionOne(){

        Console.WriteLine("优秀");
                         
      }

    Dictionary<string, EatDelegate> cl = new Dictionary<string, EatDelegate>();
          cl.Add("a",functionOne);

         foreach (KeyValuePair<string, string> a in cl)
                  {
              if(a.key==flag)

              {

                 a.Value();

                return;                      

              }

                  }              


 

posted @ 2011-01-06 00:28  一碗豆芽汤(OneV)  阅读(446)  评论(0编辑  收藏  举报