Practice mode C#

What does default(T) produce in a C# generic method?

Correct answer B. null for reference types and the zero value for value types

Explanation

default(T) returns the default value of whatever T turns out to be: null for reference types, 0 for numeric types, false for bool and a zeroed struct for other value types. It needs no constraint, which makes it the standard way to return 'no result' from generic code. Since C# 7.1 you can write just default when the type can be inferred.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.