In a world where apps target multiple platforms, Microsoft has been evolving its products and its framework in order to stay relevant. Its commitment to Open Source and enhancing developer experiences across platforms has been illustrated multiple times, the recent ones being the core CLR made open source, a full-featured free Community edition of Visual Studio 2013, the next version of open-source .NET that will also be released for Linux and Mac, Azure VMs on Linux and the next version of Visual Studio, called Visual Studio 2015 (currently in CTP 5).
This article is published from the DotNetCurry .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free and get access to hundreds of free .NET tutorials from experts
Visual Studio 2015 showcases significant technology improvements that enhances the way developers work. Some improvements include Visual C++ for cross-platform development, the new open-source .NET compiler platform, C++ 11 and C++ 14 support, Visual Studio Tools for Apache Cordova, and ASP.NET 5, including regular feature updates. As developers, we are constantly seeking ways to code efficiently and create apps and api’s that are easy to build, consistent with standards and maintainable.
A good developer is one who wants to effectively write clean and error free code. However there are a number of challenges that occur frequently in this process and hamper productivity. Some of these frequently encountered challenges are:
- Syntax Error handling
- Renaming methods, variables, etc.
- Debugging and Performance related issues.
Visual Studio provides solutions to most of these challenges and with every new release, new enhancements and features are released which makes the life of a developer easier. In the following article, we will discuss some new developer friendly features in Visual Studio 2015 which ultimately increases productivity. As of this writing, Visual Studio 2015 is available as CTP 5.
This article uses an example that contains a Class Library Project and an ASP.NET Application. The ASP.NET Application refers to the class library to make database calls. The class library further makes use of Entity Framework for SQL Server Database Table Mapping.
Let us get started.
Syntax Error Assistance for working with Missing Variable Errors
While working with .NET applications, it is a rule that we must declare a variable first, before using it. Consider the following code. We need to calculate Tax for an Employee based upon some calculations:
This code has a red squiggle indicating that taxRule is missing from the code. The lightbulb icon shown in the following image provides quick action for understanding the error and correcting it. This icon appears when you hover over the relevant code or when you have the cursor on the same line
The Figure shows quick action preview for code refactoring. taxRule here can be generated as a field, read-only field or public property. Based on the selection, the taxRule will be declared in the code and the error will be removed.
Similarly we can make use of Syntax Error Assistance for a class implementing an interface.
In the above code, the class DataAccess implements IDataAccess interface. VS 2015 IDE shows a lightbulb with quick action information which represents the methods from the interface that are not implemented by the class. On clicking on Show potential fixes at the bottom, VS 2015 offers to generate these methods for you:
The Syntax Error Assistance helps developers to reduce syntax errors that may occur in code, thereby improving productivity.
Using Code Suggestion
In VS 2015 IDE, the lightbulb icon provides code suggestions too. This provides help on possible code refactoring along with a preview for the same. E.g in a source code file, we may have several unused namespace references. As a good coding practice, we should eliminate the unnecessary namespaces as shown in the following figure.
The figure shows gray colored namespace references which are unused. The light bulb icons appears with the Code Suggestions. Drop down the icon (Ctrl + .) to see a preview of unused namespaces shown in pink. The advantage of the preview changes link is that developers get a clear idea of which namespaces are not necessary for the current code, and preview the changes before removing the unused namespaces. The figure also shows the Fix all occurrences in Document |Project | Solution option, which can be used to eliminate unnecessary references at current document, Project and Solution level.
Code Suggestion is also used to remove unnecessary code. Open the Dal.cs code file of the DataAccessLib project from the source code accompanying this article. In the DataAccess class, we have the following overloaded Get() methods:
public List Get()
var emps = ctx.EmployeeInfoes.ToList();
public EmployeeInfo Get(int id)
var emp = ctx.EmployeeInfoes.Find(id);
The above code declares inline temporary variable emps and emp to store resultant value. The methods return this temporary inline variable. We are not really using these variables for any other business operation, so as a good coding practice, we can eliminate these variables altogether using Code Suggestions Quick Actions. Right click on the emp declaration and select Quick Actions from the menu:
Clicking on ‘Quick Actions’ will bring up the Code Suggestions actions as shown here:
In the figure, we just saw that the Dark Pink highlights show possible code changes. On clicking on Preview Changes, the modified code will be displayed as shown here:
Clicking on ‘Apply’ modifies the code. We can repeat this for both Get() methods:
public List Get()
public EmployeeInfo Get(int id)
As you observed, using the VS 2015 IDE’s Lightbulb Syntax Error Assistance and Code Suggestions help you to write error free and maintainable code.
Our applications are divided into several layers (e.g. DataAccess, Business Logic, etc.) which contains classes, methods etc. in them. If we want to rename these classes and methods because of some reason, we need to check where these methods are used in the code, and rename them one-by-one. This is a time consuming step. In VS2015 IDE, Renaming has undergone an enhancement. We have been provided with Renaming Experience, which helps to easily rename types, methods, xml comments etc. in the source code.
To explore this new enhancement, open Dal.cs in DataAccessLib project and locate the Update() method. The method has the following code:
public bool Update(int id, EmployeeInfo emp)
var e = ctx.EmployeeInfoes.Find(id);
if (e != null)
e.EmpName = emp.EmpName;
e.Salary = emp.Salary;
e.DeptName = emp.DeptName;
e.Designation = emp.Designation;
The variable ‘e’ is used to represent the searched employee based on the id. In order to provide a friendly name here for ‘e’ e.g. empToUpdate, rename ‘e’ to empToUpdate. Once the rename is done, the following code change will be displayed:
Visual Studio highlights all instances and the changed variable now has a gray rectangle. A quick action light bulb is displayed on the line. The Lightbulb displays the following options of renaming:
If we select the Rename ‘e’ to ‘empToUpdate’ with… the following preview gets displayed:
The preview shows us the expected modified code. The Quick Action allows us to immediately rename the variable.
Renaming using Context Menu
We can also perform Renaming using the Context menu. To experience this, locate the Delete() method in the DataAccess class. This method has the following code:
/// the emp will be the searched Employee
public bool Delete(int id)
var emp = ctx.EmployeeInfoes.Find(id);
if (emp != null)
The code uses ‘emp’ inline variable representing employee to delete. The same emp variable is used in comments applied on the method. To rename ‘emp’ to ‘empToDelete’, right-click on the emp variable and select the ‘Rename’ option as shown in the following figure:
After Rename, the code will reflect the following effects:
As you can observe, all ‘emp’ occurrences are highlighted and on the top-right, the Rename ‘emp’ window is shown. This shows emp references with checkboxes for including Comments, strings, etc. If the Include comments is selected, the ‘emp’ mentioned in the comment on Delete method will also be selected as shown in following figure:
How cool is that! Here once we start renaming one instance of ‘emp’, the changes will be applied to all selected ‘emp’. On clicking on Apply on the Rename ‘emp’ window that we just saw, the rename will be saved. But here we also need to think of renaming conflicts. What if in the same delete method, we have one more EmployeeInfo object declared? (See the following code)
public bool Delete(int id)
var empData = new EmployeeInfo();
var empToDelete = ctx.EmployeeInfoes.Find(id);
if (empToDelete != null)
Here we have empData, a new object. If we try to rename it using Renaming using Context menu and if there is a name conflict, VS 2015 IDE will show errors:
In such cases, just press Escape to get rid of renaming conflict issues.
In the previous steps, we saw renaming inline variables. Now let’s explore the renaming feature for methods. This is a major step because methods can usually be accessed across other layers of the application. So naturally renaming in one layer, should promote renaming in the calling layers too. In our Dal.cs (Data Access Layer), we have a Get() method. This is an overloaded method and it is accessed in the EmployeeForm.aspx.cs (Presentation layer) in an ASP.NET Application. Lets’ see how the use of renaming feature in VS 2015 IDE helps rename method across layers.
Open Dal.cs in DataAccessLib project and right click the Get() method, and select Rename. Alternatively we can also use Ctrl+R, Ctrl+R shortcut key combination for this. The following box appears:
The overloaded Get() method is not selected. To select it, from the box, select the Include Overload(s) checkbox. The overloaded Get() method will get selected as shown in the following figure:
Select the Proview Changes checkbox from the top-right dialog box and rename Get() to GetData() and click on Apply button. This will show the Preview Window as shown here:
Select EmployeeForm.aspx.cs in the top section of this window and preview code changes:
The VS 2015 IDE renaming helps to perform easy code management as far as renaming of variables and methods across layers are concerned.
Code refactoring with inline local variables
In the Code Suggestion section, we have already seen how to eliminate unnecessary local variables. But consider a scenario where some methods have some logic written inside them:
public List GetData(string dname)
return ctx.EmployeeInfoes.Where(d => d.DeptName == dname).ToList();
In the above code, the logic fires query on the EmployeeInfo and retrieves Employees based upon DeptName. The result is further converted into a list. (Feel free to consider an even more complex example here.). Lets’ make the logic simpler by separating the LINQ Query code and ToList() method. Select the above code, right click and select Quick Actions:
Note: Code selected belongs to the LINQ query only.
Clicking on Quick Actions will display the following options:
Select ‘Introduce local for ‘ctx.Employeeinfoes…’’ option. The code will be modified as shown here:
The code now becomes a little simpler to understand and is divided into two steps. The queryable variable name generated can be renamed as per the steps discussed in the Renaming using context menu section of this article.
Debugging Improvements in Visual Studio 2015 IDE
Configuration of the breakpoint in VS 2015 IDE has been improved and it has been made easier and provides a new user experience. An inline toolbar has been provided using which breakpoints can be enabled or disabled. Breakpoint configuration is provided with conditions and actions. Conditions tell the debugger to only pass the breakpoint when the debugger reaches to that line of code and when defined conditions are true. Actions define the operations to take place when the breakpoint condition is true.
Consider the following code in the Dal.cs file of DataAccessLib project, where each Employees Tax is calculated based on the salary and the resultant is stored in the List of EmployeeTax object.
public List GetEmpTax()
List EmpTax = new List();
var Emps = ctx.EmployeeInfoes.ToArray();
foreach (var item in Emps)
var emp = new EmployeeTax();
emp.EmpNo = item.EmpNo;
emp.EmpName = item.EmpName;
emp.Salary = item.Salary;
emp.Tax = item.Salary * Convert.ToDecimal(0.2);
Apply the breakpoint in the emps declaration statement (4th line) and you will observe that the breakpoint inline toolbar in the IDE gets displayed:
Breakpoint can be enabled/disabled using the small circles. Click on the Settings icon to bring up the breakpoint settings:
In the above code, now set a breakpoint at the closing curly brace of the foreach loop as shown in the following figure:
Enter settings, pick window and define a condition that the breakpoint should apply when the Htt Count is 3, which means a breakpoint should get applied after the third iteration of the loop.
Run the application. Note, this method is called on the Button click event on the ASP.NET Web Form. You will observe that the breakpoint gets applied after the third iteration of the loop. This can be checked by using the EmpTax object.
One nice feature in the VS 2015 IDE is that if we delete the breakpoint accidently, we can recover it back using Undo (Ctrl+z). The conditional application of breakpoint is useful for large recursive iterations. We can keep on adding conditions even when we are running the application. The Action part of the breakpoint settings allows us to specify the actions when the condition is true. Let’s add an action that can log a message in the output window while debugging, by passing ‘emp’ to it:
And while we are at debugging, let me tell you that you can now debug Lambda expressions!
Error List Improvements
The Visual Studio 2015 IDE is improved with error lists. It shows compiler errors and code analysis warning in a single window. Compiler errors are prefixed by using CS and code analysis are prefixed using CA. The error window is displayed with compiler errors as shown here:
Improved Support for Lambda Expressions and LINQ query in the Immediate
When your code contains some large data stored in a collection, then it becomes difficult to debug and check values in that collection, if something were to go wrong. Visual Studio 2015 IDE solves this challenge by supporting Lambda Expressions and LINQ in the Immediate window. Yes you read that right! The Immediate window now allows to use Lambda Expressions and LINQ queries to filter data from collections. Lets’ implement this. Apply breakpoint on the return statement of the GetData() method of the DataAccess class in Dal.cs. Run the application. When the breakpoint is hit, open immediate window and add LINQ queries in it as shown here.
And that’s we can filter data in the immediate window from the resultant collection using LINQ .
Using PerfView Tooltip
The Visual Studio 2015 IDE, provides a great performance tooltip feature, using this we can check how much time is taken by a statement. Typically we need this in case of foreach loops. We can see the PerfView tooltip while debugging as shown here:
The above figure shows that the foreach loop takes nearly 4,142 ms.
Conclusion: This article explored some new IDE features in Visual Studio 2015 which makes developers more productive. Some of the features we explored were around suggestions for eliminating syntax errors, code refactoring, debugging, etc.
Download the entire source code of this article (Github)
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!