Casting NewType vs. Object as NewType
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Casting in programming, particularly in languages like C# and Java, is a way to convert one data type into another, often to utilize more specific operations or attributes of a derived type. At the heart of casting are two prominent approaches: casting using NewType and using Object as NewType. This article explores the technical specifics of each method, offering insights, examples, and a comparative analysis.
Understanding Casting
In statically-typed languages, variables need to be explicitly converted when dealing with different data types, which is where casting plays a crucial role. There are two primary types:
- Implicit Casting: Automatically done by the compiler when there is no risk of data loss—commonly from a subtype to a supertype.
- Explicit Casting: Required when converting between incompatible types or where data loss might occur.
Explicit casting can be further divided into:
- Casting using
NewType: This is the direct approach. - Using
Object as NewType: This is often a safer alternative as it returns anullif the cast fails instead of throwing an exception.
Casting using NewType
This approach uses a simple cast operation:
- Direct Conversion: Offers a straightforward way to cast from one object to another type.
- Error Catching in Compile-Time: Some errors may be caught earlier, especially when dealing with types that are likely incompatible.
- Prone to Exceptions: If the cast fails, a
InvalidCastExceptionis thrown, which can lead to runtime errors if not properly handled. - Safety: If the object is not of the specified type,
asreturnsnullinstead of throwing an exception, making it easier to handle errors gracefully in code. - Performance: Comparatively faster when the conversion is successful, as it does not involve exception handling.
- Additional Null Check Needed: Because a failed cast returns
null, you need to implement manualnullchecks post-cast. - Code Legibility: Can lead to an increase in conditional checks, slightly cluttering up the code readability.
Related reading
- Casting TResult in TaskTResult to System.Object
- Catch an exception thrown by an async void method
- center MessageBox in parent form
- Centering controls within a form in .NET Winforms?
- Change Assembly Version in a compiled .NET assembly
- Change C/CLI project to another framework than 4.0 with vs2010
- Change default app.config at runtime
- Change desktop wallpaper using code in .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.