Working with Nulls in C#
Learn how to create, check, access, and eliminate nulls in your C# applications.
What you'll learn
Unexpected null values and NullReferenceExceptions can be a constant source of bugs resulting in wasted time and out of hours support callouts. In this course, Working with Nulls in C#, you'll learn about the different ways that null values pop up in C# code and how to deal with them. First, you'll dive into the fundamentals of why you get null values and the difference between value and reference types. Next, you'll explore how you can use the various C# operators to check for and manipulate nulls. Then, you'll see how to think about nulls at a higher abstraction level in your object-oriented code. Finally, you'll discover how the features introduced in C# 8 can help even more by allowed non-nullable reference types. By the end of this course, you'll understand the different types of objects in C#, how to correctly create nullable value types, how to use C# operators to work with nulls in fewer lines of code, how to implement the Null Object pattern, and how to use nulls in C# 8.
Table of contents
- Introduction 1m
- Course Outline 1m
- Reference and Value Types Overview 4m
- Using Magic Numbers for Null Value Types 10m
- Introduction to Nullable Value Types with Nullable<T> 1m
- Refactoring to Nullable<T> and Removing Magic Numbers 3m
- C# Shorthand for Nullable Value Types 2m
- Nullable Boolean Values 3m
- Null and Empty Strings 2m
- Checking for Null or Empty Strings 2m
- Summary 2m
- Introduction 2m
- More on Nullable<T> 1m
- Using the HasValue and Value Properties 3m
- Using the GetValueOrDefault Method 2m
- Comparing Nullable<T> Instances 1m
- Nullable<T> Implicit and Explicit Conversions 2m
- Overview of C# Null-checking Operators 2m
- Using the Conditional Operator to Check for Nulls 2m
- Using the Null-coalescing Operator 2m
- Using the Null-conditional Operator 7m
- Using the Null-conditional Operator with Null Array Values 3m
- Thread-safe Null Delegate Invocation 1m
- Summary 2m
- Introduction 2m
- An Overview of C# 8.0 Null Features 3m
- Creating a New .NET Core 3 Console Application 1m
- Writing Some Simple Null Code 1m
- Enabling Non-nullable Reference Types for Specific Lines of Code 2m
- Enabling Non-nullable Reference Types for an Entire Code File 1m
- Enabling Non-nullable Reference Types for an Entire Project 2m
- Treating Nullable Warnings as Errors 2m
- Specifying That a Reference Should Be Allowed to Be Null 1m
- Working with Nullable and Non-nullable Properties 4m
- Working with Nullable and Non-nullable Method Return Values 2m
- Using the Null-coalescing and Null-conditional Operators 2m
- Using the Null-forgiving Operator to Override the Compiler 5m
- Refactoring Existing Code to C# 8 9m
- Nullable and Non-nullable Generics 2m
- Considerations 2m
- Summary 2m