7
var result = myObject?.GetType();

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

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
wishmaster
  • 1,303
  • 8
  • 15
  • 1
    Take a look at my article on [What's New in C# 6](https://www.simple-talk.com/dotnet/.net-framework/whats-new-in-c-6/). There I show what's happening behind the curtain. – Paulo Morgado Dec 03 '15 at 07:42

2 Answers2

9

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.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
6

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

Cameron E
  • 1,839
  • 1
  • 19
  • 20