10 C# Programming and Microsoft Visual Studio Tips and Tricks

Introduction

C# is a great language. It is relatively easy to learn with its simpler syntax over C++ programming and Java. Ten years down the line it still is a strong competitor. It has had improvement year after year, with new features added with its every release. It has not disappointed the C# developer community.

No time to read prologue, right? I know. Let's jump straight in.

  1. Environment.Newline

    Did you know that this property is platform independent and allows you to output the new line characters as per the platform?

      Console.WriteLine("My Tips On ,{0}C#", Environment.NewLine);
    

  2. Namespace Alias

    Did you know that you can substitute your big namespaces with shorter aliases? Or have you faced a situation where you had to qualify the object with its complete namespace to avoid ambiguity.

    Look at the sample below where there is a generic library created with extended .NET Framework controls.

    	  	using System.Web.UI.WebControls;
    	  	using MyGenericLibrary.UserControls;
    	  
    	   /* Assuming that you had a Text Box control in both the namespace, you would have to fully qualify  the class object with the complete namespace. To avoid that, you can use namespace alias. Change as below */
    	   	
    	  	using System.Web.UI.WebControls;
    	  	using mc = MyGenericLibrary.UserControls;
    	  
    	  /*and then use, /*
    	  	mc.TextBox textbox = new mc.TextBox();
    

  3. DebuggerBrowsable Attribute

    Every C# developer does debugging at some point or the other. This attribute is very powerful in controlling the behavior of an object during the debugging process. The debugging process involves the display of the object being debugging in a small tooltip window.

    It can be used to hide the private members, or other members that are considered to be useless in the debugging window, e.g. when you debug any class object you would see the private variables in the debugger window. You can hide it using the [DebuggerBrowsable(DebuggerBrowsableState.Never)] attribute.

      public class MyClass
      {
          private string _id;
           
          public string InternalID
          {
                get { return _id; }
                set { _id = value; }
          }
      }
    

    VISIBLE

      [DebuggerBrowsable(DebuggerBrowsableState.Never)]
      public class MyClass
      {
          private string _id;
           
          public string InternalID
          {
                get { return _id; }
                set { _id = value; }
          }
      }
      
    

    HIDDEN
  4. DebuggerDisplay Attribute

    This attribute makes the display of the variable object with a readable description. It helps users who work on your projects in the future.

    /It's simple to use. The following code shows the value of the variable.

    	  	public class MyClass
    	  	{
    	     [DebuggerDisplay("Value = {myVariable}")]
    	  	    public string myVariable = "mydisplay";
    	  	}
    	

  5. Create Virtual Directory for the Project

    You can force every developer in your team to create virtual directories with the same name for the project. This trick from the Microsoft Visual Studio IDE is extremely helpful in keeping code sync'd over several C# developer machines.

    Right-Click on the project and choose "Properties" from the options. Under the web tab, choose the "Use Local IIS Web Server" option and provide the virtual path.

    Local IIS Web Server option
    Figure 1

    Make these changes and check in your project file. All developers who use the project file will be asked to create a virtual directory with the same name in their machines.

  6. Change the Project Platform

    You can change the platform of the application that you are building and target all or specific platforms. The platforms here refer to the 32-bit and the 64-bit environments.

    Right-Click on the project and choose "Properties" from the options. Under the "Build" tab, choose the appropriate "Platform".

    Under the
    Figure 2

    Make these changes and check in your project file. All developers who use the project file will be asked to create a virtual directory with the same name in their machines.

  7. Code Definition Window

    This window lets you navigate to the Definition of an object. You can press the F12 key to quickly navigate to the definition of an object. Try it on any object in your editor right now and you shall not be disappointed.

    There is also the "CODE DEFINITION WINDOW". The key combination CTRL + W,D will bring up the code definition window for you.

    	  	 if (e.Item.ItemType == ListItemType.Item )
    	  	{
    	  	    //Your code here.
    	  	}
    	

    If you place the cursor on ListItemType word, and press the key combination you will see a window like the one below.

  8. Output Window
    Figure 3

  9. Null Coalescing Operator

    The null coalescing operator lets you compare against null in a short manner. It is represented using the double question mark notation.

    For example, the myfunction whose return value may be a nullable int. In such cases, you can use the coalescing operator to quickly check if it is null and return an alternate value.

    	  	int myExpectedValueIfNull = 10;
    	  	int expectedValue = myfunction() ??   myExpectedValueIfNull
    

  10. Shortcut for Using Statement

    Ctrl and "." brings up the window with the list of probable using statements. Use the arrow keys to select. Press enter. Voila! A using statement is added.

  11. Voila! A using statement is added
    Figure 4

  12. Dreadful Dataset Merge Errors

    Have you been unable to figure out why the dataset merges failed? There is a way out.

    Yes, you have to wrap your code in try-catch. But watch out for the specific code in the exception handling block that captures the exact reason for the merge to fail.

       	StringBuilder error Messages = new StringBuilder();
       	try
       {
                 		DataSet dataSet1 = populateDataSet(1);
        		DataSet dataSet2 = populateDataSet(2);
       
       		dataset1.Merge(dataset2);
                           }
                           catch (System.Data.DataException de)
                           {
          foreach (DataTable myTable in dataSet1.Tables)
          {
             foreach (DataRow myRow in myTable.GetErrors())
             {
                foreach (DataColumn myColumn in myRow.GetColumnsInError())
                {
                    //loop through each column in the row that has caused the error
                    //during the bind and show it.
                     error Messages .Append(string.Format(
                      "Merge failed due to  : {0}", myColumn.GetColumnError(myColumn)));
                }
             }
          }
                          }
     

  13. Conclusion

    I hope you've enjoyed these C# programming and Microsoft Visual Studio tips and tricks. If you have some tips and tricks you'd like to share please leave them in the comments or email them to me directly. Thanks for reading.

    Related Articles





About the Author

Srinath M S

I would love to leave a footprint in this flat world

Comments

  • Instruments and developing throughout Irvine - swimwear simply leaves without adios

    Posted by Abughibungunc on 05/08/2013 04:02am

    Different questions about golf replied to in addition to the reasons you would need to start reading every statement in this write up. [url=http://www.golfjapan.co/]タイトリスト[/url] What pros generally are not alleging concerning golf and in what way this impacts on you actually. [url=http://www.golfjapan.co/アイアン-c-2.html]二木ゴルフ[/url] Brand new publishing unearths the concept about golf and additionally why you need to take action right now. [url=http://www.golfjapan.co/レディース-c-11.html]女子ゴルフ[/url] Infrequent study delivers the main points over golf which just some visitors know. [url=http://www.golfjapan.co/ブランドから選択-c-364.html]ナイキゴルフ[/url] Supplies and developing throughout Mexico - golf basically leaves without hasta la vista FrbAgjQjqFlr [url=http://adidasyasuijp.com/]アディダス[/url]NmhCzmBzdSjs [url=http://www.adidasyasuijp.com/jeremy-scottジェレミースコット-c-4.html]adidas jeremy scott[/url]WqyAddHnsCma [url=http://www.adidasyasuijp.com/adidasランニング-c-7.html]adidasランニング[/url]CtwZjmPkyYrr [url=http://adidaskutujp.biz/]アディダス adidas[/url]ArlTsbNitWip [url=http://www.adidaskutujp.biz/jeremy-scott-ジェレミースコット-c-4.html]adidas jeremy scott[/url]JfsWanHinEoq [url=http://www.adidaskutujp.biz/adidas-original-c-9.html]アディダス ランニング[/url]QwfIanNkmVpm LkqCvdNhqIev QjfQqgBvbBkc [url=http://nikekutujp.biz/]ナイキ[/url]YgdMqcNvbRgz [url=http://www.nikekutujp.biz/nike-air-force1エアフォース1-c-14.html]ナイキ エアフォース[/url]BylSteLqtPtg [url=http://www.nikekutujp.biz/nike-air-maxエアマックス-c-12.html]ナイキ エアマックス[/url]RyeWoiHrzCko [url=http://www.nikekutujp.biz//nike-air-jordanエア-ジョーダン-c-13.html]ナイキランニング[/url]TggRqgAjhBoz

    Reply
  • Brief piece of content reveal the indeniable information about chloe and exactly how it can certainly cause problems for people.

    Posted by emeseesip on 05/07/2013 05:29am

    The Most Complete nike Guide You Ever Seen Or else Your Cash Back [url=http://www.guccija.biz/]グッチ 財布[/url] Ok, amazing item. Users got to consider adidas instantly while it is still up for grabs ! ! [url=http://www.guccija.biz/]グッチ 長財布[/url] adidas assists every one of us by adding several exclusive features and characteristics. It's a unvaluable item for every supporter of nike. [url=http://www.guccija.biz/]グッチ ショルダーバッグ[/url] Unbiased statement uncovers 3 new stuff surrounding adidas that no-one is discussing. [url=http://www.chanelja.biz/]シャネル 財布[/url] This is why absolutely no one is speaking of gucci and as a consequence know what one should take care of today. [url=http://www.chanelja.biz/]chanel バッグ[/url] Creative questions about adidas have been answered and therefore reasons why you must definitely read in detail every term in this expose. [url=http://www.chanelja.biz/]財布 chanel[/url] The basics behind nike which you may reap the benefits of commencing today.[url=http://www.nikeja.biz/]nike[/url] The best way to understand just about every thing there is to understand concerning adidas in Three basic steps.

    Reply
  • Unique guide supplies the run information on the mizuno which just a couple of visitors know of.

    Posted by icoppyapedcap on 04/25/2013 04:17pm

    XewOxvKofCov [url=http://www.nikeyasuijp.com/]ナイキ[/url]CxgXwrPduNbc [url=http://www.nikeyasuijp.com/nike-air-force1エアフォース1-c-14.html]nike air force[/url]PzbHemEgmZjv [url=http://www.nikeyasuijp.com/nike-air-maxエアマックス-c-12.html]ナイキランニング[/url]YnuUavUmkZgb [url=http://www.nikeyasuijp.com/nike-air-jordanエア-ジョーダン-c-13.html]nike free[/url]ApuOqyDowWuq

    Reply
  • Other VS keyboard tips

    Posted by heya on 09/24/2010 03:45pm

    Great article, I learned some new tips.
    Some keyboard shortcuts I can suggest that I find really useful in Visual Studio are:
    Shift + F12 : finds all references; for example, find all locations of where an instance of your class is declared or used
    Ctrl + K, I : use this over a method name to see its declaration as opposed to hovering over it with your mouse
    Ctrl + K, P : use this over your method parameters, to see all the overloaded method signatures
    ALT + right arrow : use this after the "." dot operator to view all accessible members of your object
    CTRL + D, Q : use this during debugging at a breakpoint on an object to open a Quick Watch window where values can be easily viewed and edited or reevaluated

    I find Microsoft's keybinding posters to be really useful and they can be found by doing a search of "Visual Studio Poster" at
    http://www.microsoft.com/downloads

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds