Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference types vs Nullable types ToString()

Tags:

Could someone please be kind enough to explain why calling ToString() on an empty reference type causes an exception (which in my mind makes perfect sense, you cant invoke a method on nothing!) but calling ToString() on an empty Nullable(Of T) returns String.Empty? This was quite a surprise to me as I assumed the behaviour would be consistent across types.

Nullable<Guid> value = null; Stock stock = null; string result = value.ToString(); //Returns empty string string result1 = stock.ToString(); //Causes a NullReferenceException 
like image 833
Maxim Gershkovich Avatar asked Aug 03 '12 07:08

Maxim Gershkovich


People also ask

What is null ToString ()?

ToString(null) returns a null value, not an empty string (this can be verified by calling Convert. ToString(null) == null , which returns true ). However, passing a null variable indeed equals to null, so that object v = null; Convert. ToString(v) == string.

Is reference or nullable type?

Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.) So, no they're not reference types.

What is the point of nullable reference types?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

What is nullable reference?

Nullable reference types are a compile time feature. That means it's possible for callers to ignore warnings, intentionally use null as an argument to a method expecting a non nullable reference. Library authors should include runtime checks against null argument values.


2 Answers

Nullable<T> is actually a struct that has some compiler support and implementation support to behave like a null without actually being null.

What you are seeing is the collision between the implementation allowing you to treat it naturally as a null as you would any other reference type, but allowing the method call to happen because the Nullable<T> isn't actually null, the value inside it is null.

Visually it looks like it shouldn't work, this is simply because you cannot see what is done in the background for you.

Other such visual trickery can be seen when you call an extension method on a null reference type... the call works (against visual expectation) because under the hood it is resolved into a static method call passing your null instance as a parameter.

How does a Nullable<T> type work behind the scenes?

like image 118
Adam Houldsworth Avatar answered Oct 20 '22 16:10

Adam Houldsworth


Nullable is a value type and the assignment to null causes it to be initialized with Value=null and HasValue=false.

Further, Nullable.ToString() is implement as follows:

public override string ToString() {     if (!this.HasValue)     {         return "";     }     return this.value.ToString(); } 

So what you are seeing is expected.

like image 34
logicnp Avatar answered Oct 20 '22 16:10

logicnp