C Sharp (C#) - Polymorphism and Abstract Classes
Identify the error in this code snippet:
object obj = 123; string s = obj as string; Console.WriteLine(s.Length);
object obj = 123; string s = obj as string; Console.WriteLine(s.Length);
as cast with incompatible typesobj holds an int (123), casting it as string returns null.s is null, calling s.Length causes a null reference exception.as cast will fail and s will be null, causing a null reference exception. -> Option Bas returns null on failure; check before use [OK]as cast before use [OK]is fixes null issuess for null before accessing membersas works with value types15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions