Code Editor

The Code Editor is where productivity really starts to come into its own with the concept of light bulbs. Light bulbs (which in prior incarnations of Visual Studio were called Smart Tags) pull together the collective intelligence of the IDE into one central, inline spot where code fixes, refactorings, and code completion suggestions can be provided and acted on quickly and easily.

A good example of the light bulb in action can be found when trying to implement an interface. Normally, implementing an interface is a fairly code-intensive task. We have to individually create a member within the implementing class to map to each member defined on the interface. With the light bulb concept, we can let the code editor itself do a lot of the work for us. To implement our interface on an existing class, we would add the “implements” syntax like this.

class Program : IContosoConsole { }

As we type this into the code editor, Visual Studio’s IntelliSense (more to come on this technology in a bit) will automatically flag the interface name with a red underline indicating that potential problems exist. This is because we have not yet followed through on the requirements of the interface. In other words, we have established the “this class implements a specific interface” syntax, but we haven’t actually gone ahead and implement the interface members. Figure 7.12 shows our initial syntax along with the IntelliSense underline/highlight.

Image

FIGURE 7.12 The Code Editor catching potential issues with an interface implementation.

If we now hover our cursor over the interface name that has been highlighted for us, we will get a brief summary of what the code editor has identified issue wise, along with the light bulb icon to the left of the issues window (see Figure 7.13). In this case, the editor has correctly identified that our implementing class does not (yet) implement all the required interface members.

Image

FIGURE 7.13 An issue identified with the light bulb.

If we click on the light bulb or select the Show potential fixes link within the issues window, we will have a set of actions that Visual Studio recommends to fix the identified issue.


Note

The light bulb indicator is also accessible in the left margin of the code editor. It works the same way there that it does when you hover over a flagged coding problem: it produces a drop-down list of issues and fixes when you click on it.


In this case, the actions are Implement Interface and Implement Interface Explicitly.

These two options will essentially write the code for us necessary to implement our interface using either an implicit or an explicit style:

Image Implement interface—Member names do not reference the name of the derived interface.

Image Implement interface explicitly—Members are prefixed with the name of the derived interface.

In fact, fixes are visually presented to you within the editor, allowing you to get an actual preview of the code changes that will happen if you select one of the options. See Figure 7.14 to view the light bulb in action.

Image

FIGURE 7.14 Code fixes with in-line preview using the light bulb.

This type of fix is known as a “Generate from Usage” scenario. It’s meant to solve a fairly common workflow issue: with certain development styles (especially, test-driven development or TDD), it is often the case that you are referencing members and types that have not yet been created. In the preceding example, this is an interface, but it could just as easily be another class, a property, or a method.

Light bulbs are a common aid for any type of coding issue ranging from actual syntax errors in the code to refactoring to missing references to more involved usage scenarios (such as the one we just discussed).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset