C # Programming and Visual Studio usage tips (top)

Source: Internet
Author: User
Not interested in the prologue? OK, let's go straight to the chase, here are 10 C # programming and Visual Studio IDE usage tips.

1, Environment.NewLine

Do you know that this property is platform-independent? Allows you to output new newline characters based on each platform.

Console.WriteLine ("My Tips on, {0}c#", environment.newline);

2. Namespace aliases

Do you know that you can use shorter aliases instead of long namespaces? Have you ever encountered the need to limit the full namespace to avoid ambiguity? Look at the following code example, which is a common library created using the extended. NET framework controls.

Using System.Web.UI.WebControls; Using Mygenericlibrary.usercontrols;     /* Assuming this had a Text Box control in both the namespace, you would has to   fully qualify the class object wi Th the complete   namespace. To avoid this, 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 Properties

Every C # developer should have experience with program debugging, the ability to control object behavior during debugging is very powerful, it displays the object in a small prompt window during debugging, it can be used to hide private members or display in the Debug window is also redundant members, for example, when you debug a class object, You can see the private variables in the Debug window, you can use the [DebuggerBrowsable (Debuggerbrowsablestate.never)] property to hide them, and the following is the visible code.

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

Here's the code that hides it:

[DebuggerBrowsable (Debuggerbrowsablestate.never)] public class MyClass {   private string _id;         public string Internalid   {      get {return _id;}      set {_id = value;}   } }

4. DebuggerDisplay Properties

This property allows a variable object with a readable description to be displayed, which helps to provide the efficiency of the other members of the team in reading the code in the future, and its usage is very simple, and the following code example shows the value of the variable.

public class MyClass {   [DebuggerDisplay ("Value = {myvariable}")] public   string myvariable = "Mydisplay";}

5. Create a virtual directory for your project

You can force each developer to create a virtual directory with the same name for the project locally, and this technique from the visual Studio IDE will help the code synchronize between the computers of multiple C # developers. Right-click the project name, select Properties, and on the Web tab, select the Use local IIS Web server option, and then specify a virtual path for it.

When this is set, all developers who use the project file will receive a request to create a virtual directory with the same name on the local machine.

6. Change the project platform

You can change the application's build target platform, where the platform refers to 32-bit and 64-bit environments, right-click on the project name, select "Properties", and in the "Build" tab, select the desired target platform as shown in.

7. Code Definition Window

This window allows you to jump to the definition of the object, you can press the F12 key to quickly jump to the object's definition position, in the code Editor of any object to try this function, I believe will not let you down. There is also a special Code Definition window that pops up a Code Definition window when you follow the Ctrl+w,d key combination.

if (E.item.itemtype = = ListItemType.Item) {   //your code here.}

If you hover the cursor over the ListItemType and press the key combination, you will see a window as shown.

Figure 2 Code Definition window

8, NULL merge operator

The null merge operator allows you to compare null values in a very concise way, which is represented by a two question mark. For example, the value returned by MyFunction may be an empty integer value, in which case you can use the merge operator to quickly check whether it is empty, and then return a substitute value.

int myexpectedvalueifnull = 10;int Expectedvalue = MyFunction ()?? Myexpectedvalueifnull

9. Using statement shortcut keys

Press CTRL +. A list of available using statements will pop up, use the arrow keys to move, and press ENTER to confirm the selection.

10. Finding the root cause of a scary data set merge error

Have you ever encountered a reason why you can't find a dataset merge error? Now there is a way to surround your code with Try-catch, preferably by observing the output of a particular code in the exception handling block, which captures exactly why the merge failed.

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 of 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)));}}}           

Summary

I hope you have the flexibility to use these C # programming and Visual Studio skills, to enjoy the fun of writing code, we have a lot of communication, progress together

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.