C#编程和Visual Studio使用技巧(上)

时间:2022-01-26 03:02:21

对开场白没兴趣?好吧,我们直接切入正题,下面介绍10个c#编程和visual studio ide使用技巧。

1、environment.newline

你是否知道这个属性是与平台无关的?允许你根据每个平台输出新的换行字符。

?
1
console.writeline("my tips on ,{0}c#", environment.newline);

2、命名空间别名

你是否知道可以使用更短的别名代替长的命名空间?你是否遇到过需要限制完整的命名空间以避免产生歧义?看下面的代码示例,它是使用扩展的.net framework控件创建的一个通用库。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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属性

每个c#开发人员应该都有过程序调试的经历,这个属性在调试期间控制对象行为的能力非常强大,在调试过程中它在一个小提示窗口中显示对象,它可以用于隐藏私有成员或在调试窗口中显示也是多余的成员,例如,当你调试类对象时,在调试窗口中你可以看到私有变量,这个时候你就可以使用[debuggerbrowsable(debuggerbrowsablestate.never)]属性来隐藏它们,下面是可见的代码。

?
1
2
3
4
5
6
7
8
9
10
public class myclass
 {
   private string _id;
     
   public string internalid
   {
      get { return _id; }
      set { _id = value; }
   }
 }

下面是使之隐藏的代码:

?
1
2
3
4
5
6
7
8
9
10
11
[debuggerbrowsable(debuggerbrowsablestate.never)]
 public class myclass
 {
   private string _id;
     
   public string internalid
   {
      get { return _id; }
      set { _id = value; }
   }
 }

4、debuggerdisplay属性

这个属性可让具有可读描述的变量对象显示出来,它有助于提供团队其它成员未来阅读代码的效率,它的用法也是非常简单的,下面的代码示例显示了变量的值。

?
1
2
3
4
5
public class myclass
 {
   [debuggerdisplay("value = {myvariable}")]
   public string myvariable = "mydisplay";
 }

5、为项目创建虚拟目录

你可以强制每个开发人员在本地为项目创建一个同名的虚拟目录,这个来自visual studio ide的技巧将有助于代码在多个c#开发人员的电脑之间同步。在项目名称上点击右键,选择“属性”,在“web”选项卡中,选中“使用本地iis web服务器”选项,然后为其指定一个虚拟路径。

这样设置后,所有使用该项目文件的开发人员都会收到一个要求,在本地机器上创建一个同名的虚拟目录。

6、改变项目平台

你可以改变应用程序的生成目标平台,这里的平台指的是32位和64位环境,在项目名称上点击右键,选择“属性”,在“build”选项卡中,选择需要的目标平台,如下图所示。

 C#编程和Visual Studio使用技巧(上)

图1 修改项目的目标平台

7、代码定义窗口

这个窗口允许你跳转到对象的定义,你可以按f12键快速跳转到对象的定义位置,在代码编辑器的任意对象上试试这个功能,相信一定不会让你失望的。此外,还有一个专门的代码定义窗口,当你按照ctrl+w,d组合键时就会弹出一个代码定义窗口。

?
1
2
3
4
if (e.item.itemtype == listitemtype.item )
 {
   //your code here.
 }

如果你将光标停留在listitemtype上面,然后按下组合键,你将会看到如下图所示的一个窗口。

C#编程和Visual Studio使用技巧(上)

图2  代码定义窗口

8、null合并运算符

null合并运算符允许你以很简洁的方式比较空值,它使用两个问号表示。例如,myfunction返回的值可能是一个空的整数值,在这种情况下,你可以使用合并运算符快速检查它是否为空,然后返回一个代替值。

?
1
2
int myexpectedvalueifnull = 10;
int expectedvalue = myfunction() ?? myexpectedvalueifnull

9、using语句快捷键

按下ctrl+.会弹出一列可用的using语句,使用箭头键进行移动,按下回车键确认选择。

10、寻找恐怖的数据集合并错误根本原因

你是否遇到过无法找出数据集合并错误的原因?现在有办法了,使用try-catch将你的代码包围起来,最好是在异常处理块中观察特定代码的输出,可以准确捕捉到合并失败的原因。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)));
      }
     }
   }
           }

小结

希望你能灵活运用这些c#编程和visual studio技巧,享受写代码的乐趣,大家多多交流,一起进步