Practice mode C#

What does the as operator return in C# when the conversion fails?

Correct answer A. null

Explanation

obj as T attempts a reference, nullable or boxing conversion and returns null if obj is not compatible with T. A cast written as (T)obj throws InvalidCastException instead. Because failure yields null, as can only be used with reference types or nullable value types, so x as int does not compile while x as int? does. Modern code often prefers obj is T t.

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.