zoukankan      html  css  js  c++  java
  • C# TryGetValue

    在写的代码中遇到这样的一段Code

       [Route("api/[controller]/[action]")]
        [ApiController]
        public class JDOrderController : ControllerBase
        {
            [HttpPost]////仓库实物库存查询
            public async Task<IActionResult> Stock_queryStock(string method2)
            {
                   IActionResult result = null;           
                    //RouteData:Gets the Microsoft.AspNetCore.Routing.RouteData for the executing action
    //得到Microsoft.AspNetCore.Routing.RouteData用于执行操作
    //
    Values: Gets the values produced by routes on the current routing path.
    //获取由当前路由路径上的路由生成的值。
    //value 存的是一个dic的键值对里面是有controller和action,而且程序执行到这里,里面都有存值:JdOrder和Stock_QueryStock
    //tryGetValue返回时的bool值,有值就是true.
    //同时这里也考察了下这个Out的用法,将参数带进去,改变参数的值以后再带出来
                    bool b=   RouteData.Values.TryGetValue("controller", out object controllerName);
                    bool bb = RouteData.Values.TryGetValue("action", out object actionName);
                    //
                    string controller = controllerName.ToString();
                    string action= actionName.ToString();
                    string Content = RequestInfo(method, MethodCont);
                    result = new ContentResult()
                    {
                        ContentType = "application/json",
                        Content = JsonConvert.SerializeObject(Content)
                    };
                     return result;
           }              
         }

    这个TryGetValue,百度了一圈是跟Dictionary有关的

    关于Dictionary.TryGetValue的个人理解记录

    如果字典中不含有指定key,out value会返回一个适当的默认值。
    Dictionary<TKey, TValue>.TryGetValue Method (TKey, TValue)
    判断字典中是否含有TKey,如果含有,则TValue返回TKey的值,否则返回一个合适的默认值,比如0/false/null

    (bool)(UserFields.TryGetValue(fieldName, out finfo))可将其转为boo类型,
    它方便的是避免了判断key知否存在而引发“ 给定关键字不在字典中。”的错误。
    可以通过下面的测试来更进一步了解:

    static void Main(string[] args)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("aaa", "123");
                dic.Add("bbb", "456");
                dic.Add("ccc", "789");
                dic.Add("ddd", "321");
    
                //获取与指定的键相关联的值,
                //虽然这里给ourStr已经是赋值了的,但是执行到dic的时候,就是会去找键:'aaa'的值,找到了outStr就是值,找不到就是null值
                string outStr = "999";
                //上面的dic中并没有ttt,所以下面的outStr会是NULL值,
                //是避免了判断key知否存在而引发"给定关键字不在字典中."的错误。
                dic.TryGetValue("ttt", out outStr);          
    
                Console.WriteLine(outStr + "<br />");
                dic.TryGetValue("bbb", out outStr);
                Console.WriteLine(outStr + "<br />");      
    
                Console.ReadKey();
            }
    string outStr = "999";
    //上面的dic中并没有ttt,所以下面的outStr会是NULL值,
    //是避免了判断key知否存在而引发"给定关键字不在字典中."的错误。
    dic.TryGetValue("ttt", out outStr);
    dic.TryGetValue("ttt", out string str);
    关于out的两种声明都是一样的
    无论是在参数外声明,还是在参数内声明都是ok的

      

  • 相关阅读:
    asp.net 文件下载
    net 数据库连接详解 相当经典啊
    取值:webconfig中的key
    通过监听的端口查找本机进程和服务思路
    以系统服务运行某个程序
    测底根除Windows流氓软件开机自动运行
    使用Wireshark在主机上抓取远程主机的数据流量
    记录Windows远程登录日志
    证书不匹配发出告警的解决方法
    WPS office云同步图标彻底删除方法
  • 原文地址:https://www.cnblogs.com/ZkbFighting/p/14096377.html
Copyright © 2011-2022 走看看