This video examines the new C# refactorings available in Visual Studio 2017.
- [Instructor] Most development shops nowadays think about refactoring their code as they go through the development process. And to aid in this refactoring, there's a number of tools available that help you analyze the code, suggest refactorings, managing the refactoring process itself. Visual Studios had these tools for years. And they're available for Visual Basic, Ce# and Ce++. In this movie, we'll look at the enhancements to the Ce# refactorings and I'll start by looking at the method on line 11. Watch what happens when I move my cursor to line 13.
You'll see that a light bulb appears over there. That's called the quick action. So I move off that line, the light bulb disappears. When I click on that line, it reappears. And that's telling me that there's something that Visual Studios analyzed about this line and they're going to suggest a code change. In this case, a refactoring, there's another clue on this line that's telling me there's a refactoring. Do you see it, it's under the new keyword. There's three gray dots, so it's telling me that Visual Studios analyzed my line of code where I've instantiated this book class and it sees that immediately after the line of code that I instantiate the class, I set a number of properties.
So the refactoring tool thinks that I can change this code and the name of this refactoring is object initialized can be simplified. What you're seeing here is the quick action. And then over here, you're seeing what will change. The red is the current text and the green is the modified text, so it's going to remove a number of semi colons, add some commas, put some curly braces in, indent my code. If I am happy with that, I can preview the changes.
And then apply them or I could've just double clicked on the quick action. So here's the improved version using air quotes there. So it's virtually the same here except it doesn't have a semi colon on the end and I got a curly brace, end curly brace, and then I've lost the b.title, b.publication date so if you like this simplification, if you think this is a simpler way of looking at your code, use this refactoring. The next one is suggested here on line 26.
Do you see the three dots there? There's something about this datetime variable that needs a refactoring, so let's put our cursor here. And see what it is, before I look at the refactoring, what do we know about this variable? It's a datetime and I'm using it as an out parameter in this tryparse method. If you've worked in tryparses, you know that you get a Boolean back from the method call. And in order to get the actual date back, you have to pass in an out parameter, so that's what we're doing here using the out keyword.
And you always have to declare your variable outside the if statement and then you can use that inside the if statement. But in Ce# 7.0, you can actually declare the variable inline, inside the if statement. So let's look at this refactoring. Click on the quick action, it says it's an inline variable declaration. I'll just double click on it and you'll see that it removed that variable and then it added just this one word here, datetime.
For the third refactoring, let's look for the three dots in this line, you see them? Nah, you can't see them cause they're not there. In this case, the quick action doesn't have any suggestions, it's available, but it's not putting the three dots there to bring my attention to this line. And if I click on it, I don't see the quick action light bulb, but I know that there's this new refactoring. So let me right click on this and choose quick actions. And the name of this refactoring is convert to interpolated string.
What this is going to do is take out the string.format and these placeholders and instead, it's going to use the new Ce# syntax, I think this came out in Ce# 6.0. Where you put the dollar sign in front of the string and then you put the parameters within the string body itself, so let's do that. So the next one's called the simplifying null checks and throw expressions using the null coalescing operator and this is one of the refactorings that Microsoft suggests is available.
But on my build the Visual Studio, doesn't seem to work. I'm not getting a light bulb when I look at this throw line. But I'll show you what it's supposed to do if it works. The idea is you're checking this inbound parameter for null and if it is null, then you're throwing an argument null exception. I'm using the name of operator inside Visual Studio to retrieve the name of this parameter. And then once I have the book, then I get the title and put it in the string variable.
There's a shorter syntax in the newer version of the Ce# where you can use the null coalescing operator like this. You can say bestselling.title null coalescing, throw argument null exception. So if this is null, then go ahead and throw this exception, otherwise get the title. And that refactoring is supposed to do it but as I said, it's not working on my machine. Another refactoring is the sync file name and type. Here's an example, let's go look at this file, it's pub.cs and inside pub.cs I have a class called publication.
The refactoring choices I have is rename the file to the publication.cs or rename the type to pub. I'll choose this first item and now the file name has changed. And for the final refactoring, we'll look inside the book class, inside the book file, I should say, not the class, inside this file I've got some enums. Now a lot of developers like to keep their enums in separate text files and in this case, they're inside the same text file as the book class.
So to refactor these out, I can right click. I can choose the move type the buyertype.cs, double click, you notice that disappears out of the file. And there is now a new buyertype.cs file. And let's do the same thing with this cover type and then save my project. So what you've scene is a number of new refactorings that are available in the editor. And that you can get to all of these refactorings by right clicking and choosing the quick action.
And in some of the cases, you'll get a light bulb when you're on a line of code where it's suggesting the refactoring.
Released
3/8/2017- Installing Visual Studio 2017
- Using debugging features
- Reviewing document navigation enhancements
- Examining IntelliSense Improvements
- Using XAML tools
- Reviewing the tooling added to support Docker containers
- Debugging without the hosting process
- Using the Visual Studio 2017 Installer
Share this video
Embed this video
Video: C# and VB refactorings