Does null inherit from Object as well in C#?
Where does the null itself defined?
Does null inherit from Object as well in C#?
Where does the null itself defined?
Does null inherit from Object as well in C#?
Unlike in some other systems, null is not defined in terms of a class or an instance. On the contrary, it signifies the absence of any instance, and it doesn't have a type. It is implicitly converted to whatever reference type fits the context. The widest conversion (i.e. when no other can be inferred) is to object. But still, null is not generally of type object.
null is not an object - it is a language keyword that denotes the lack of an object reference.
Where does the null itself defined?
null is a keyword. null is not a type.
There are two kinds of variables, each with its own set of nullability rules.
The variable is typed and can refer to instances of the same or different types.
//The reference type is System.Object and
// the instance type is System.String
object s = "123";
With reference type variables, null indicates an absence of an instance.
//The reference type is System.Object and
// there is no instance.
object x = null;
Using a reference type variable that has no instance will result in a null reference exception.
string s = null;
s = s + "a"; //BOOM.
Value type variables have a single type, and the values assigned must be of this type and no other.
int i = 3;
With certain value type variables (only those that are of the type Nullable<T>), null may be assigned. This indicates the absence of value.
int? i = null;
Using a value type that has no value does not generate null reference exceptions - there is no reference involved.
int? i = null
if (i < 3) //false
null is not a type, it is a value. More specifically it is a value that is by definition the abscence of a value. In C# null is convertible to any reference type.
So null does not inherit from System.Object but it is a value convertible to any object which does not inherit from System.ValueType.
null is of the "null type", which does inherit from object.
See ECMA-334 11.2.7