kunal-chowdhury.com

Do you know about Reference Counter in #VisualStudio 2013?


If you have already installed and used Visual Studio 2013, you might already noticed that every class and method has “x references” marked on top of it, where ‘x’ is a counter. These are called as “Reference Counter” and provides you information about the uses.

 

It’s a new feature in Microsoft Visual Studio and will definitely help the developers to find out the referenced count easily. In this blog post, we will discuss about this in detail. Keep reading.

 

What’s new in Visual Studio 2013?

If you are new to Visual Studio 2013, you may want to check the below posts first:

 

Reference Counter

Visual Studio 2013 has a no. of new features and enhancements for the developers to make their work easy. One of them is “Synchronization of appearance, theme, settings etc. across all your devices” that you use and logged in with your Live ID credentials. Among other new enhancements, one is easy access to know how many places you used your class, methods etc. Let’s see it in a piece of demo code.

 

Let’s assume, we have a class called Demo and there is a static method (to demonstrate it easily) named Print(). By default you will see them marked with “0 references” at the top of each declaring line. This means, you did not yet use the class and the method. Have a look into the following example:

 

ZERO Reference of the method and class

 

Now call the Print() method of the class from Program class and you will immediately notice that the reference count automatically increased to one and showing as “1 reference”. This means, you used it in only one location. Have a look at it here:

 

ONE References of the method and class

 

Let’s call it again. You will notice that the count has again increased and now showing it as “2 references” on top of each declaring line. Here is how it will look in this case:

 

TWO References of the method and class

 

You can see in the above examples that reference count of both the class and method is increasing by one in each call of the method. This is because, we are using a static call of the method. If we had a normal method “Print()” and if we called it by creating a single instance of the class “Demo”, in that case, the reference count of the class and method would be different. For example, calling the Print() method two times in a single instance of Demo class would generate “1 reference” count for the class and “2 references” count for the method.

 

The settings for the reference counter is enable by default but in case you did not find the behaviour in your Visual Studio 2013 IDE, you can easily enable it by going to the options page. To enable/disable it, go to Tools –> Options and then navigate to Text Editor –>All Languages –> Code Information Indicators at the left pane of the dialog. In the right pane, you will find a parent check box labelled “Enable Code Information Indicators” and a child check box labelled “Show References”. To enable this reference counter in your code file, both of them needs to be checked. In case, you don’t want it, just uncheck the child check box labelled “Show References”.

 

How to enable Reference Indicator in the Code Information

 

This new feature/enhancement will definitely help the developers like you to easily check the total nos. of places the class or method has been used. Isn’t it? Say thanks to the Visual Studio developer team for such a nice feature.

 

Stay tuned to my blog to know more about the other new features in Visual Studio 2013. I am available in Twitter and Facebook, don’t forget to connect with me there and say a “Hi”. This blog post was composed targeting the preview version of the IDE and hence this might change a little in the final product of the Visual Studio 2013 edition, which is very rare.


Kunal Chowdhury
If you have come this far, it means that you liked what you are reading (Do you know about Reference Counter in #VisualStudio 2013?).

Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions.

Authored Books: