Highlight syntax of generic types in Visual Studio

Issue

Take a look at the following code:

public class MyClass<T>
{
  ...
}

Working with Java or C++ in eclipse, the T would be highlighted, as it is a generic template.

How can I achieve this functionality in Visual Studio?

Solution

As @Michael Sander mentioned, User Types would provide this functionality, but only in an inconvenient and partial manner. Assuming this functionality is working for you, here is how you would color generic types as you wish.

The ‘usertypes.dat’ file must be created by you as a basic text file, and it must be saved to the same location as the Visual Studio executable. Once created, list each of your desired “Generic Type Names” on individual lines. An example would like like so:

T
TIn
TOut
TEntity

Afterwards, save the file and restart Visual Studio.
Then Head over to TOOLS -> Environment -> Fonts and Colors and look for

User Types
User Types (Type parameters)

You can now set that color to the color you desire. It is recommended to set both of them to the same color so it works for return types and such.

=====

As an alternative solution, Resharper, a non-free Visual Studio extension, provides something akin to this functionality.

The extension allows you to color “Type Parameters”. If you were hoping to only color Generic Type parameters, you are unfortunately, out of luck. But if you do not mind having all type parameters retain the color you desire, that is possible.

If you own Resharper, you can enable this feature like so:

Head over to TOOLS -> Environment -> Fonts and Colors and look for

ReSharper Type Parameter Identifier

This will change any Type parameters to the color of your choice.

While purchasing this extension solely for this problem is likely not a desirable solution, those who already own it will hopefully find this helpful.

Answered By – JesseNewman19

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.