daily pastebin goal
53%
SHARE
TWEET

C#7 Dictionary Scoping issues

a guest Dec 7th, 2017 345 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #r "$NuGet\System.Net.Http\4.3.3\lib\net46\System.Net.Http.dll"
  2. #r "$NuGet\System.Security.Cryptography.Algorithms\4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll"
  3. #r "$NuGet\System.Security.Cryptography.Encoding\4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll"
  4. #r "$NuGet\System.Security.Cryptography.Primitives\4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll"
  5. #r "$NuGet\System.Security.Cryptography.X509Certificates\4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll"
  6.  
  7. var dict = new Dictionary<string, IEnumerable<string>>
  8. {
  9.     ["token"] = new List<string> { "abc" }
  10. };
  11.  
  12. if (dict?.TryGetValue("token", out var values) == true)
  13. {
  14.     Console.WriteLine(values.First());
  15. }
  16.  
  17. // Produces unassigned local variable error, scoping issue?
  18. {
  19.     var errDict = new Dictionary<string, IEnumerable<string>>
  20.     {
  21.         ["token"] = new List<string> { "abc" }
  22.     };
  23.  
  24.     if (errDict?.TryGetValue("token", out var tokens) == true)
  25.     {
  26.         Console.WriteLine(tokens.First());
  27.     }
  28. }
RAW Paste Data
Top