How is C# NULL different from vb.net Nothing?
Console.WriteLine(Nothing = "") => True
vs
Console.WriteLine(null==""); => False
My understanding was that both null
and Nothing
are same. But above code clearly explains it is not.
What is the equivalent of C# null
in VB.NET?
In computer programming, null is both a value and a pointer. Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.
Some of the most common use cases for NULL are: a) To initialize a pointer variable when that pointer variable hasn't been assigned any valid memory address yet. b) To check for a null pointer before accessing any pointer variable.
In C , the definition of NULL and 0 is equal.
No its not the same as null means a value that is unavailable unassigned or unknown and zero is a defined value.
In your code, VB guesses that you are comparing String
s, since one of the operands is a String
. In String
comparisons, Nothing
is equivalent to the empty String
""
. It then does a value comparison, which returns True
.
Use Is
to compare references:
Console.WriteLine(Nothing Is "") '=> False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With