#1,213 – Visual Studio 2015 – Unused using Statements Greyed Out

In Visual Studio 2015, the editor will grey out any using statements that are not required.  This is done at build time.  Note that you can still use the Remove Unused Usings command to remove any unused statements.  (This command has been renamed Remove Unnecessary Usings).

Below, all but the first using statement is greyed out, since the code that follows makes no use of types from any of the other namespaces.




#1,185 – Managing using Directives, part II

You can add missing using directives by using the Resolve command.  You can also clean up the current list of using directives in a file, removing the ones that are no longer needed.

You can remove unneeded using directives by clicking anywhere within a file and selecting Organize Usings | Remove Unused Usings.

In the example below, we start out with 20 using directives at the top of the file.


We then select Remove Unused Usings.


After we execute this command, we’re left with only four using directives at the top of the file.


#1,184 – Managing using Directives, part I

As you write code, Visual Studio will let you know that you’ve used an identifier that it doesn’t know by marking it with a red squiggly underline.  Below, we’ve started creating a class that derives from Shape.  But Visual Studio tells us that it doesn’t know about Shape.


The easiest way to resolve this is to try right-clicking on the unknown identifer and selecting the Resolve entry.  Visual Studio will give you a list of namespaces that it can find the identifier in.  You can then select one of these options and Visual Studio will add the relevant using directive.




Note that this will only work if the identifer is valid somewhere within the assemblies that your project has referenced.  If you have correctly spelled an identifer and the Resolve option does not appear, you will need to add a reference to the assembly where the identifier in question is defined.



#849 – Using the Call Stack in Visual Studio to Navigate within Your Code

When in break mode within Visual Studio, you can view the call stack in the Call Stack window.

When you bring up the Call Stack window, there will be a yellow arrow pointing to the top of the call stack, indicating the location of the next statement that will execute when you resume execution.  The Locals window will show the values of local variables within the current method.


At this point, you can double-click on another method within the call stack.  When you do, the code editor will show code for that other method and the Locals window will show local variables for the same method.  They will have the values that existed at the point that the method at the top of the call stack was called.  Notice that a yellow arrow still indicates the execution point, but a green arrow now appears, showing the current method being examined.


#848 – Viewing the Call Stack in Visual Studio

The call stack keeps track of the currently executing method in your application, and from where that method was called.  You can use the debugger in Visual Studio to view the current call stack when you are in break mode (at a breakpoint or stepping through your code).

If the call stack is not already visible, click on the Debug menu, then Windows and Call Stack.  (Or press Ctrl+D, C).


In the example below, we’ve started the application and are located in our Main method, which was called by native code.


If we now step into the Dog.BarkYourAge method, the call stack shows this method on the top of the stack, with Main as the second entry.  Main is just below BarkYourAge in the stack, because it called BarkYourAge.


If BarkYourAge then calls DogUtil.GenerateBark and we step into that method, we see:


If we return from GenerateBark, we see:


#770 – Use Intellisense to Get List of Methods to Override

In a derived class, you can override a member of the base class if it’s declared as virtual.  In Visual Studio, the Intellisense feature can help you to discover the methods that can be overridden.

Suppose that we have the following definition for Dog:

    public class Dog
        public virtual string Prop1 { get; set; }
        public string Prop2 { get; set; }

        public virtual void Method1()

        public void Method2()

Now when we are adding code to a child class of Dog, we can just enter the keyword override, followed by a space, to see a list of candidate members that can be overridden.


To add the code for one of these members, just use the down arrow to select the method and then press TAB.  The body of the method will be generated for you.


#766 – Adding an Interface to a Class Diagram

If you add a class to a class diagram and the class implements one or more interfaces, you’ll see the interfaces show up on the class with the standard interface designator (a short line with a circle at the end).



You can also add an element to the class diagram that represents the interface itself.  To do this, just right-click on an interface and select Show Interface.



Once you do this, the interface will be added to the diagram.  You can then expand the basic element to see the members of the interface.


#765 – Adding Base or Derived Classes to a Class Diagram

You can use the class diagram to follow the inheritance chain for any class currently in the diagram, moving up to the class’ base class or down to the classes that derive from the class.

For example, let’s say that you’ve dragged the ArgumentException class onto a class diagram, from the Class View.



You can add the base class of ArgumentException to the diagram by right-clicking on the class and selecting Show Base Class.



The base class will now be shown on the diagram.



You can also show all derived classes of a particular class by right-clicking on the class and selecting Show Derived Classes.



Using this technique, you can easily browse through the classes in the .NET Framework.


#764 – Expanding All Classes in a Class Diagram

By default, when you add types to a class diagram, they are displayed collapsed, showing only the name of the type and the category (e.g. class, interface).


You can expand individual types by clicking on the double downward facing arrow.  You’ll then see all the members of the type.

You can use the “+” key on the numeric keypad to expand the currently selected type.  You can also do the following, to quickly expand all types:

  • Select all typeson the diagram by pressing Ctrl+A
  • Expand all types by pressiong the “+” key on the numeric keypad
  • Right-click on the diagram and select Layout Diagram

Selecting Layout Diagram will reorganize all types so that they are layed out in a nice way.  The only problem is that types that are not connected to other types in the diagram will once again be collapsed.  You’ll need to expand and manually lay out these types.


#763 – Fixing a Class Diagram that Cannot Find Types

If you create a class diagram that contains types found in assemblies that your project references, and then you leave the class diagram open when you close Visual Studio, you may see some errors in the class diagram when you re-open the solution.  The types on the class diagram may all be drawn in red and report “Type cannot be found”.



You can fix this problem by just closing and re-opening the class diagram.  The diagram will re-discover the information for the types.