Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null in c# value type or reference type

Tags:

c#

null

Originally I had this impression that NULL is reference type because it is assigned to references, then encountered this concept called nullable value types, this makes my theory in an awkward situation, so is NULL value type of reference type on earth ?

like image 960
zinking Avatar asked Jul 16 '13 02:07

zinking


People also ask

IS NULL 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.

Is NULL and '\ 0 the same?

'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.

IS NULL considered true in C?

NULL means "nothing", neither true or false or a boolean value or int or string.

IS null pointer in C?

Master C and Embedded C Programming- Learn as you goA null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.


2 Answers

The C# specification is clear on this point: the null keyword represent an expression that has no type. It is neither reference type nor value type nor pointer type. However that expression is convertible to all of those types. There is no requirement that the compiler classify all expressions as having a type, and in fact it does not.

Now you might then ask at runtime what is the type of a null reference, what is the type of a null value of nullable value type, and what is the type of a null pointer. The answers are:

  • a null reference does not have a type. If you have a box that can hold chocolates it is pointless to ask "what brand of chocolates are in this empty box?" A missing chocolate does not have a brand; a null reference does not have a type.

  • similarly, the type of a null pointer is, again, not anything; null pointer values don't point to anything. If you have a piece of paper that has the address 1600 Pennsylvania Avenue on it, you can ask "what is the colour of the house at this address", and get the answer: white. If you have a blank piece of paper, asking what the colour of the house at that address is doesn't get you any useful answer.

  • a null value of a nullable value type is a value of the nullable value type.

like image 187
Eric Lippert Avatar answered Sep 20 '22 21:09

Eric Lippert


null isn't a type.

From msdn:

The null keyword is a literal that represents a null reference, one that does not refer to any object.

Nullable types are instances of the Nullable<T> struct which "Represents a value type that can be assigned null."

like image 43
Jason P Avatar answered Sep 17 '22 21:09

Jason P