Tip:
Highlight text to annotate it
X
Hello Ace
This is RetroTK2 and welcome to this C# tutorial
Today, we're taking a quick look at another extension for Visual Studio called ReShaprer
You'll need a fair knowledge of C# to fully appreciate this video Ace
But I use this extension all the time, so it's important that I introduce it to you as soon as possible
So let's get started...
Type ReSharper into Google and it should be the first link
Click the big download button to download the 30 day trail
I have a paid license for ReSharper 8 Ace
But the features I'll show are the same in 10
Let's look at the pricing
Click on “For Individual Customers” and you'll see that ReSharper has adopted a subscription pricing model
$129 is a steal for how much time this extension saves
Other licenses come with different JetBrain's products such as dotCover and dotMemory
I'd say that for the extra $20 these products are definitely worth it Ace
But they're out of the scope of this video
Once it is downloaded, click the .exe and follow the installer
If you've any previous versions of Visual Studio installed, I would advise that you deselect these from the install menu
Accept the license agreement and wait for it to install
With it installed, launch Visual Studio and close the license information dialog window
A dialog should pop up asking which shortcuts scheme you want to use
I only ever used the ReSharper ones Ace, but feel free to try Visual Studio's
Now to show you what ReSharper can do, I'm going to create a very quick console project Ace
Visual Studio will create the program file with a lot of unused using statements at the top
Unused code will appear lighter and can usually be removed safely
Click on the usings and press “Alt” + “Enter” to bring up the options menu
Get used to that shortcut Ace, it's used constantly
With the menu showing, click “Enter” on “Remove unused directives in file”
All the unused using statements should disappear
Let's write some code
Say I create a class called “MyClass”, and a method called “MyMethod” and create an instance of a class that doesn't exist yet
Well if I select the code and press the magic shortcut “Alt” + “Enter”, we can create the type
Select “Create type 'MyNewClass'” and the type will be generated below
You can use this trick with methods and constructors as well
I add the parameter I want in my constructor, in this case a string
Select the code, press “Alt” + “Enter” and Press “Enter” on the “Create constructor 'MyNewClass'” option
This will create a constructor in the “MyNewClass” class, complete with a sting parameter
You can press “Alt” + “Enter” while over the “myString” parameter to initialize a private field as well
But I did it the long way round Ace
Here I'm creating the private field
Press “Alt” + “Enter” on it, and then press “Enter” on “Initialize field from constructor”
This will add the new parameter to the constructor and initialize the field from the parameter
Doing it this way means the constructor is left with a parameter that we're not using and since the signature of the constructor has changed
We're getting an error where we created the “MyNewClass” instance
If this was a larger code base, there could be thousands of places where this constructor was being use
So this method is definitely not the safest
Fortunately here it's easy to fix
Click on the “MyNewClass” instance and navigate to the “Change signature...” option
From here we can remove the “myString” parameter
Doing so removes the parameter and also removes the string we've passed to the constructor
Re-adding the string will remove the error
Bit of a backward way of doing things Ace, but we got there in the end
Let's change the parameter's name
Click on “myString” and press “F2”
If this is the first time you've used a Rsharper shortcut, you may get a shortcut dialog popping up
Check the “Apply to all ReSharper Ultimate shortcuts” box and select one of the choices
I can then rename the parameter from the dialog box
ReSharper will also warn you if you have any unnecessary code
Here I have a redundant field initialization, indicated by the blue line
Click on the affected code, press “Alt” + “Enter” and select “Remove field initializer”
ReSharper can automatically format your code
If I press “Ctrl” + “Alt” + “F” the format code dialog shows up
The Default “Full Cleanup” should be good enough, but you can create your own custom one if you like Ace
Press “Enter” and the code will be nicely formatted
Now I'll show you how easy it is to define a method
Here I create a class called “MyNewestClass”and create an instance of it in the main method
Then I call the method that I want to create
Press “Alt” + “Enter”, click “Create method 'MyNewestClass.Run'”
And my method is created in the MyNewestClass
ReSharper will also try and import any using statements that the code requires
Say I create a List, ReSharper will bring up a tooltip with the correct List suggestion
If I click off it, I get the error and a tip telling me to import “System.Collections.Generic.List”
Pressing “Alt” + “Enter” will import it for us
The scroll bar is different with ReSharper
Here I can navigate to warnings and errors in my code by clicking on the icon at the top of the scroll bar
The error also shows up as a red line on the bar
If I press “Shift” + “Delete” on a line the whole line will be removed
Let's look at unused code
Here I'm calling “MyMethod” from the public method “Run”
The method's text is in full color
If I comment out the line with “Ctrl” + “/”
“MyMethod” goes a lighter color indicating that it's not being used in the code
Now I can remove this code with confidence, knowing that it is not being used in my code base
Let's put these classes into separate files
I'll bring up the solution explorer so that you can see this better Ace
Click on the class you want to move and press “Alt” + “Enter”
Select “Move to 'MyClass.cs'” and ReSharper will create a new file for the class and put it in that file's directory
I'll do this with the other classes and as you can see in the solution explorer, the classes have been put into their respective files
Now that they're in separate files, I can show you how easy it is to navigate between them
If you press and hold “Ctrl” and click on a class or method, it will take you to where that class or method is defined
You can also find the usages of that method or class in your code
Right Click on the method or class and select “Find Usages”
This will jump to that usage if there is only one of them
If there are multiple usages, a window will open with the usages organized by files
From here you can click on a usage and it will jump you to it
Let's look at naming conventions
Say I declare a constant string called “myNewConstant”
Now because this naming convention doesn't match ReSharper's one, it'll flag up as a warning
If we click on it and press “Alt” + “Enter”, we can select “Rename to 'MY_NEW_CONSTANT'”
Now say I wanted to change this naming convention, Press “Alt” + “Enter”
Select “Inspection 'Inconsistent Naming'”, and then select “Change naming rule 'Constant fields (non private)...'”
A dialog will pop up and from here you can change the naming convention
You can also do it from the options menu if you prefer
Click on “ReSharper > Options” and under “Code Editing” select “C#”
Click “Naming Style” and from here you can change the naming conventions for your code
Let's change the color scheme so that we're using ReSharper's
Click “ReSharper > Options > Code Inspection > Settings” and click “Enable Code Analysis”
Make sure “Color identifiers” are enabled
It would seem that ReSharper 10 has already enabled mine Ace
Probably remembering my settings from ReSharper 8
You can also highlight the current line by enabling it
Click “Editor” under the “Environment” menu and select “Editor Appearance”
Click the “Highlight current line” box to enable it
There's still a lot of things to cover with Reshaprer Ace, but hopefully this is enough to us started
And that's it...
Rate, Comment and Subscribe Ace, I hope you're enjoying the new year
You can email me at RetroTK2@gmail.com
With any of the extensions that you're using
Thank you for watching Ace, and I'll see you in the next video