Sometimes, while working, we face some silly problems which are very easy to solve by nature, but we can’t figure that out at that moment and waste some valuable time finding their solutions. Similar happens to some other scenario, where we knew what to do to get a certain result, but can’t memorize particularly. I was thinking to make a list of these small things in my blog, which will might be helpful for myself and may be to other audiences(I hope :)). I am planning to create different article for different type of development environment. Today, I will explain about some such very quick/small .NET tips and tricks, for visual studio environment. I usually use visual studio 2010.
Show Line Numbers On VS Editor:
I still remember, when, at the very beginning of my familiarity with visual studio, I was often forgot how to show the line number on my code editor. By default, it isn’t enabled by visual studio. But its somehow very important to remember it in a long file of code. What you will have to do, is to go to “Tools->Options->Text Editor->All Language->General” and check the box titled ‘Line numbers’ under ‘Display’ section. See the image below for more accurate idea:
If you need to do this for a specific programming language, you can do so as well. Just find this similar settings under that specific programming language(like c#), instead of ‘general’.
Using ‘Region’ And ‘If’ Directives:
Did you know, we can use these directive in various useful scenario to keep our code manageable and easy to debug. These can be used in any programming language that .NET supports. There are few more directives though, these two seems very useful to me. These directives are used anywhere in the code file and starts/ends with ‘#’ character. Please check here to see details about more directives.
Using the region block, you can combine the related properties/methods/code block in a single collapsible/expandable section. In this way, if you use it through the file, even a long coded file also becomes easy to traverse. here is a simple example how to write this:
#region All code related flying //all codes go here #endregion #region All code about eating //codes go here #endregion
notice the above image, no matter how long the class is, if you organize that is some such regions, you will find more comfortable to work with them time to time. Finding a code block also becomes super easy.
in the similar way, the ‘if’ directive helps you to create some extra code segments of code for a particular condition. Suppose, you need to execute some code in debug mode, but not in release mode. Here this directive will be very helpful.
#if DEBUG //debug code goes here #endif
Another interesting thing here is, this will show you whether the code block is currently active or not. Such as, if you are in debug mode and using code like above, the inner codes will be normal like other codes. But, if you change settings to release mode, this code block will become gray, like commented code, mentioning this won’t be executed in current settings.
Downgrade Projects From Visual Studio 2010 To Visual Studio 2008:
This is a scenario, when you may don’t have updated visual studio on your PC but need to open solution(s)/project(s) created on updated version. Luckily, downgrading visual studio 2010 to visual studio 2008 is pretty straight forward/easy. open your solution file(with .sln extension) in notepad or similar editor, find the visual studio version at the top of the file, which should be ‘11.0’ and change that to ’10’. Also, change the ‘visual studio 2010’ to ‘visual studio 2008’ also. And you are done. The solution and related projects will now be opened in visual studio 2008 easily. This worked for me very well, without any issue. If you are having any issue while doing this, let me know details via commenting, I will try to get a solution asap and refine this section.
For your information, I will be extending this article day to day to add other related tips as well. So, if you have any similar .net tips and tricks in mind, please let me know by commenting on this article or via contact page. I will try to attend them and integrate on this article as soon as I can. Happy coding 🙂 .