防止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(); }
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。