-1

Why does one use not null and not empty when checking for session variables? Why not just use not empty? I am looking at this page and came up with this question.

http://www.dotnetfunda.com/forums/show/8994/create-login-session

Samson Bujju
  • 615
  • 1
  • 6
  • 11
  • I'm not aware of a not empty for `Session` - the only check I've ever seen or used is `If Not Session("SomeKey") = Nothing Then`.... – Tim May 17 '14 at 05:46
  • That page you linked to doesn't have any instances where the Session is checked for null or empty... – Jeremy Thompson May 17 '14 at 05:47
  • possible duplicate of [Checking session if empty or not](http://stackoverflow.com/questions/7172910/checking-session-if-empty-or-not) – Jeremy Thompson May 17 '14 at 05:47
  • Are you talking about checking `Session` or a `String`? If it's a `String`, you do both because `null` (`Nothing` in VB.NET) is not the same thing as an empty string (""). – Tim May 17 '14 at 05:48
  • @Tim I am getting a string from a textbox and setting it as a session variable. So, do I have to check for both ("" and nothing) ? – Samson Bujju May 17 '14 at 05:53
  • @SamsonBujju - If you want to make sure you're putting something in the `Session` that isn't null or empty, then yes, check for both. null and empty are not equivalent. – Tim May 17 '14 at 05:55
  • @Tim Thanks for the clarification. I come from c# background. isn't null and "" the same in c#? – Samson Bujju May 17 '14 at 05:56
  • @SamsonBujju - No, they're not. `null` is nothing. An empty string is an empty string - it's empty, but it's still *not* nothing. See http://social.msdn.microsoft.com/Forums/vstudio/en-US/0e7c2041-ee72-4c54-a9f5-f1617e88325a/what-is-the-difference-between-1-null-2empty-and-3-in-c?forum=csharpgeneral for some good discussion on this. – Tim May 17 '14 at 05:58
  • @Grant Winney, sorry it was by mistake, i wanted to paste this one : http://stackoverflow.com/questions/7172910/checking-session-if-empty-or-not – Krunal Patil May 17 '14 at 05:59

1 Answers1

0

String.Empty and "" are almost the same, both refer to an existing string that has no content.

Said almost because, "" creates a temporary string in memory (to have something to compare against) while String.Empty is a language constant.

On the other hand, null means nothing, no object at all.

In more familiar terms, String.Empty is like having an empty drawer while null means no drawer at all!

more information : http://social.msdn.microsoft.com/Forums/vstudio/en-US/0e7c2041-ee72-4c54-a9f5-f1617e88325a/what-is-the-difference-between-1-null-2empty-and-3-in-c?forum=csharpgeneral

Kumar Manish
  • 3,746
  • 3
  • 37
  • 42