Working with Nulls in C#

Learn how to create, check for, access, and eliminate nulls in your C# applications.
Course info
Rating
(161)
Level
Intermediate
Updated
Sep 8, 2016
Duration
1h 25m
Table of contents
Description
Course info
Rating
(161)
Level
Intermediate
Updated
Sep 8, 2016
Duration
1h 25m
Description

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're going to learn about the different ways that null values pop up in C# code and how to deal with them. First, you're going to learn the fundamentals of why you get null values and the difference between value and reference types. Next you'll learn how you can use the various C# operators to check for and manipulate nulls. Finally, you'll learn how to think about nulls at a higher abstraction level in your object-oriented code. 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 with fewer lines of code, and how to implement the Null Object pattern to remove the need to write repetitive null checking code.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Working with Files and Streams in C#
Intermediate
3h 13m
12 Oct 2018
Error Handling in C# with Exceptions
Intermediate
1h 37m
6 Aug 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone, my name is Jason Roberts. Welcome to my course, Working with Nulls in C#. I'm a Microsoft. NET MVP, freelance developer, and author. In this course, we're going to learn about the different ways that null values pop up in C# code and how to deal with them. Some of the major topics that we'll cover in this course include the fundamentals of why we get null values and the difference between value and reference types, how we can use the various C# operators to check for and manipulate nulls, and how to think about nulls at a higher abstraction level in our object-oriented code. By the end of this course, you'll understand the different type of objects in C#, how to correctly create nullable value types, how to use C# operators to work with nulls with fewer lines of code, and how to implement the Null Object pattern to remove the need to write to repetitive null checking code. Before beginning the course, you should be familiar writing basic C# code in Visual Studio. I hope you'll join me on this journey with the Working with Nulls in C# course at Pluralsight.

Working with Nullable Value Types and Strings
Hi, I'm Jason Roberts from Pluralsight. Welcome to this course Working with Nulls in C#. In this first module, we're going to look at how we can work with nullable value types and also null and empty strings. So we're going to kick off this module by getting a brief recap on the difference between reference and value types in a C# code. We'll then look at one method of representing nulls in value types, and this is to use magic numbers. We'll see how this is a bad practice and some of the problems that it can cause. Next we'll see a solution for creating nullable value types without using magic numbers, and that's to take advantage of the nullable of T type. Once we understand how to use nullable of T, we'll see the C# shorthand that allows us to define them in our code. We'll learn about another example of using nullable of T, this time with Boolean values. So for example, we may be receiving nullable Boolean values from a backhand database. Finally, we'll take a look at the concept of null strings in C#, and we'll also cover the convenience methods to check for empty or strings containing only white space.

Accessing and Checking for Null Values
Hi, welcome back. In the previous module we learned about the difference between value and reference types, and also how we can represent null value types by using the Nullable of T struct. In this module we're going to be learning more about accessing and checking for null values. So in this module we're going to start off by learning a bit more about Nullable of T. So we're going to learn about some of the convenience properties and methods that the Nullable of T struct exposes, and how we can use them in our code. We'll take a brief look at comparing Nullable of T instances, and what happens when one of the instances is null, and we'll also learn when we need to explicitly cast from a Nullable of T to the underlying value type. We'll then take a look at the various C# operators that we can use when we're working with null values. So we're going to start off by looking at the general purpose conditional operator, and even though we can use this, it creates somewhat cumbersome syntax. We'll then look at a couple of null specific operators, we'll start off with the null-coalescing operator, and then we'll take a look at the null-conditional operator that was introduced in C# 6. Finally, we'll see an example of using the null-conditional operator to check for null values in a delegate before we invoke them, and we'll see how when we use the null-conditional operator it reduces the amount of code that we need to write. So let's kick off this module by looking at some of the convenient properties and methods available to us when we're using Nullable of T.

Eliminating Null Reference Exceptions
Hi, welcome back. In the previous modules we've been learning about the built-in support in C# and. NET for nulls. In this final module we're going to look at how we can actually use object-oriented programming techniques that we can implement in C# to eliminate null reference exceptions. In this module we're going to start off by introducing a design pattern called the Null Object pattern, and we'll start off by looking at some reasons why we might want to consider using the Null Object pattern itself. We'll then get a high-level overview of the pattern itself, before jumping into some demos. So in the first demo we're going to see some example code that doesn't use the Null Object pattern, and we'll see that when we run this code we experience a runtime null reference exception, and to fix this code we can go and add manual null checking code. We'll then go and refactor this implementation to start using the Null Object pattern. In this first implementation of the Null Object pattern, we're going to be using a base interface as an abstraction to represent both real and null objects. Once we understand how to implement the Null Object pattern using interfaces, we'll go and refactor this to instead use a base class, and we'll see that when we do this we can also create a single null object instance that we can use in our code. We'll wrap up the module by looking at some considerations to bear in mind when we're implementing and using the Null Object pattern, and some instances when the Null Object pattern doesn't make sense. So let's kick off this module by getting an introduction to the pattern.