惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
P
Palo Alto Networks Blog
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
WordPress大学
WordPress大学
S
Schneier on Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy International News Feed
Security Latest
Security Latest
NISL@THU
NISL@THU
Cyberwarzone
Cyberwarzone
I
Intezer
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
博客园_首页
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
博客园 - Franky
月光博客
月光博客
GbyAI
GbyAI
G
Google Developers Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
T
Troy Hunt's Blog
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 司徒正美
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
博客园 - 叶小钗
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
G
GRAHAM CLULEY

博客园 - 一碗豆芽汤(OneV)

C#一些基础知识回顾 2021年立个Flag,回顾十多年来的IT职业之路-----没事瞎BB 使用 Visual Studio 创建 .NET 5 控制台应用程序 log4net将日志进行分类,保存到不同的目录当中 .net 使用PowerShell获取电脑中的UUID .Net MVC中访问PC网页时,自动切换到移动端对应页面 C# 全角符号转半角 微信开发第二篇:工具篇 微信开发第一篇:问题篇 [转载]ODAC (odp.net) 开发到部署 【项目开发】LigerUI+MVC的应用 VS中两个常用辅助工具 sqlite中常见的问题总结 第1次做面试官的随谈(二) 第1次做面试官的随谈(一) 转,有用 [转载收录]ASP.NET 2.0加密Web.config - 一碗豆芽汤(OneV) - 博客园 重构的例子 关于重构之Switch的处理【一】如果是有序的话,如何处理
关于重构之Switch的处理【二】 - 一碗豆芽汤(OneV) - 博客园
一碗豆芽汤(OneV) · 2011-01-06 · via 博客园 - 一碗豆芽汤(OneV)

前面写了一个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;                      

              }

                  }