From the course: Visual Studio Developer Tips
Unlock this course with a free trial
Join today to access over 22,400 courses taught by industry experts.
Null propagation - Visual Studio Tutorial
From the course: Visual Studio Developer Tips
Null propagation
- [Instructor] How many times have you run into an unexpected null reference exception when you're working in .NET, or C# or Visual Basic? It happens more often than it should, at least it does in my case. So the tip that I want to look at today is a language feature that debuted in C# 6 called the null propagation operator. And here's how it works. Here's the situation you use it in. On line 16, I have a variable of type tour, that's set to null, and then on line 18, I have a string variable. What I'd like to do on line 26 is get the tourName property and store that in this string variable. Because tour1 is null, this line of code will throw an exception. We typically will write code like this to protect ourselves. We'll write a no-statement that says, if tour1 equal equal the null, then return. That's sometimes called a guard clause. Using the null propagation operator, we can reduce some of that. So first let me comment this out, Control + K, Control + C. Instead, what we'll do…
Contents
-
-
-
Brace matching in C#4m 57s
-
Select vertical sections of code with Box Selection5m 50s
-
Move code with keyboard shortcuts2m 49s
-
Find duplicate code with the code clone analysis tool4m 8s
-
Paste JSON/XML as class3m 42s
-
Colorize output in the build window4m 37s
-
A better way to redock a tool window3m 57s
-
The Open Command Line extension3m 39s
-
Get consistent editor settings with EditorConfig3m 46s
-
Git at a glance: Updates to the Visual Studio status bar4m 1s
-
Surround a block of code with a code snippet2m 5s
-
Understand the document tab features3m 35s
-
Clipping to a border using an opacity mask5m 53s
-
Insert Line with Ctrl Enter2m 21s
-
Use the File Tab channel3m 31s
-
Use the Add New File extension4m 27s
-
Keyboard access for CodeLens4m 32s
-
Use the Clipboard Ring2m 56s
-
Use the GlyphFriend extension5m 41s
-
Activate Open File in Solution Explorer3m 14s
-
Use the enhanced scrollbar41s
-
Drag and drop in Solution Explorer5m 20s
-
Keyboard shortcuts for MVC views and controllers4m 17s
-
Pin data tips in debug window6m 6s
-
The Trailing Whitespace Visualizer extension4m 19s
-
Date rounding and other helpers5m 21s
-
Memorize window layout (Reuse)4m 26s
-
Use DialogResult6m 48s
-
Label a breakpoint2m 31s
-
The Image Optimizer extension2m 35s
-
Use Ctrl+Spacebar to fix case of variable names3m 40s
-
Use the nameof operator6m 21s
-
Discover untested code with code coverage7m 36s
-
Use the Hand tool in XAML designer2m 16s
-
Array Visualizer extension2m 35s
-
Use Ctrl+Tab to cycle through windows3m 18s
-
Understand line stroke patterns4m 37s
-
The Comment Remover extension3m 41s
-
Toolbox tip shortcuts6m 1s
-
Randomize data with LINQ4m 13s
-
Customize context menu4m
-
Get the file path1m 25s
-
Convert lists4m 34s
-
Enum flags5m 19s
-
Reference highlighting2m 4s
-
Evaluate performance with PerfTips4m 50s
-
Null propagation4m
-
Multiple editor views of same file2m 17s
-
Customize the output window7m 37s
-
Colored braces with the Viasfora extension4m 15s
-
Use the C# interactive window10m 47s
-
The Smart Semicolon extension3m 29s
-
Reference Highlighting1m 35s
-
IEnumerable Extension Methods4m 58s
-
Compare LiNQ sequences6m
-
The Clear Recent Project List extension3m 34s
-