Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 6 null propagation what value is set when object is null

var result = myObject?.GetType();

In this scenario what would be the value of Result if myObject is null?

like image 855
wishmaster Avatar asked Dec 03 '15 01:12

wishmaster


2 Answers

Assuming your object does not hide default object.GetType definition: GetType returns Type, which is a reference type, so null will be returned, and result will be inferred to be of type Type.

If your object has a method which does hide object.GetType, it will also return null, but type inferred for result might change: it will either be TResult if that method returns reference type TResult, or Nullable<TResult> if it returns a value type of type TResult.

like image 69
MarcinJuraszek Avatar answered Sep 18 '22 10:09

MarcinJuraszek


The result should be null because the ? operator short circuits the operation.

like image 38
Cameron E Avatar answered Sep 19 '22 10:09

Cameron E