code_junkie

Which is correct way to check for Null exception?

Nov 11th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
  2. HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
  3. }
  4.  
  5. if (HttpContext.Current != null)
  6. if (HttpContext.Current.Response != null)
  7. if (HttpContext.Current.Response.Cookies != null)
  8. if (HttpContext.Current.Response.Cookies[authCookieName] != null)
  9. HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
  10.  
  11. if(HttpContext.Current != null &&
  12. HttpContext.Current.Response != null &&
  13. HttpContext.Current.Response.Cookies != null &&
  14. HttpContext.Current.Response.Cookies[authCookieName] != null)
  15. {
  16. // do your thing
  17. }
  18.  
  19. private bool CheckSuspendersAndBelt()
  20. {
  21. try
  22. {
  23. //ensure that true is true...
  24. if (true == true)
  25. {
  26. //...and that false is false...
  27. if (false == false)
  28. {
  29. //...and that true and false are not equal...
  30. if (false != true)
  31. {
  32. //don't proceed if we don't have at least one processor
  33. if (System.Environment.ProcessorCount > 0)
  34. {
  35. //and if there is no system directory then something is wrong
  36. if (System.Environment.SystemDirectory != null)
  37. {
  38. //hopefully the code is running under some version of the CLR...
  39. if (System.Environment.Version != null)
  40. {
  41. //we don't want to proceed if we're not in a process...
  42. if (System.Diagnostics.Process.GetCurrentProcess() != null)
  43. {
  44. //and code running without a thread would not be good...
  45. if (System.Threading.Thread.CurrentThread != null)
  46. {
  47. //finally, make sure instantiating an object really results in an object...
  48. if (typeof(System.Object) == (new System.Object()).GetType())
  49. {
  50. //good to go
  51. return true;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. return false;
  62. }
  63. catch
  64. {
  65. return false;
  66. }
  67. }
  68.  
  69. if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
  70. HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
  71. }
Add Comment
Please, Sign In to add comment