Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null(In C#) Vs Nothing(in vb.net)

Tags:

c#

vb.net

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?

like image 835
BetterLateThanNever Avatar asked May 26 '17 01:05

BetterLateThanNever


People also ask

How NULL is defined in C?

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.

Why do we use NULL in C?

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.

Is zero and NULL the same in C?

In C , the definition of NULL and 0 is equal.

Are NULL and 0 the same?

No its not the same as null means a value that is unavailable unassigned or unknown and zero is a defined value.


1 Answers

In your code, VB guesses that you are comparing Strings, 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
like image 109
SSS Avatar answered Sep 23 '22 09:09

SSS