C# NullReferenceException Demystified
Learn Principles, Features, and Strategies to Avoid NullReferenceException in Your C# Code
"Object reference not set to an instance of an object."
If you have found that scary exception message, you're not alone.
As C# software developers, we all have found it at some point.
I've found it dozens of times while working with past employers and clients on codebases of different sizes and complexities.
And often when we find it, the only thing we could do is fire up the Visual Studio debugger, go through the code line by line, and inspect all parameters and variables until finding the "object reference not set..." Boring and time-consuming. Arrrggg!
That's NullReferenceException in C#.
The "Billion-dollar mistake."
If you want to get rid of this exception from your code, this is the course for you.
In this course, you will learn principles, features, and strategies to say goodbye to NullReferenceException—in just 1 hour and 5 minutes, to be precise.
Ready for a preview? Click the video at the top of the page.
These are the modules and lessons inside:
- Introduction (1min)
- Getting Started
- Principles, Features, and Strategies (1h3min)
- Value Types vs Reference Types
- What NullReferenceException Is
- Exercise: NullReferenceException and Collections
- Check and Prevent Null
- Exercise: Check and Prevent Null
- Use C# Nullable Operators
- Use C# Nullable References
- C# Nullable References and Arrays
- Exercise: Fix Nullable Warnings
- Use Option Wrapper
- Use Option Utility Methods
- Prevent NullReferenceException When Using FirstOrDefault
- Exercise: Fix NullReferenceException When Using FirstOrDefault
- Prevent NullReferenceException by Separating State
- Exercise: Solve a Real NullReferenceException Issue
- Conclusion (1min)
- Wrap-up!
After finishing this course, you will:
- Understand when NullReferenceException is thrown.
- Use Nullable Operators to simplify null checks.
- Work with Nullable References to identify troublesome code blocks.
- Adopt the Option type as an alternative to Nullable References.
- Safely use LINQ FirstOrDefault method.
No expertise required to write code free from the NullReferenceException.
You only need to be comfortable declaring and initializing objects and working with conditionals, loops, and collections such as arrays and lists.
Click the "I Want This" button and let's get rid of this exception once and for all. See you in the first lesson!