防止ToString() 没值报错/TryGetValue

 

           Dictionary<string, object> keyValuePairs = new Dictionary<string, object>()
            {
                { "1","a"},
                { "2","b"},
             
            };

            var str = string.Empty;
            object ret;

            var end= keyValuePairs.TryGetValue("1", out ret);

            str= ret?.ToString();

            if (string.IsNullOrEmpty(str))
            { 
            
            }

 

            var str = string.Empty;
            object ret;
            var end = keyValuePairs.TryGetValue("1", out ret);
            str = ret?.ToString();

            if (string.IsNullOrEmpty(str))
            {
            }
bool xs = "q".Equals("Q", StringComparison.OrdinalIgnoreCase); bool sx = "q".Equals("Q"); string sd = null; var q = sd?.ToString(); string sd2 = ""; var q2 = sd?.ToString(); int? xx = null; int? intCanNull = null; var hasValues = intCanNull.HasValue; if (hasValues) { int num = intCanNull.GetValueOrDefault(); } else { int num = intCanNull.GetValueOrDefault(); }

 

posted @ 2022-05-27 14:31  ProZkb  阅读(70)  评论(0编辑  收藏  举报