Visual Studio常用代码片段整理(一)

使用方式:if后两下Tab键

1.if

if (true)
{

}

 

2.#region   代码块

#region MyRegion

#endregion

 

3.checked   检查数据是否发生了溢出(参考:https://www.xin3721.com/ArticlecSharp/c6701.html)

checked
{

}

上述是对整个代码块的数据溢出检查,checked还有对单个变量的检查如

 int i = int.MaxValue;
 int m = checked(i + 10);

以上代码运行时会再执行 int m = checked(i + 10); 报错(因为变量m超出了int类型的最大值),如果不加checked,则数据溢出也不会抛出,此时m=-2147483639

 

4.class   快速新建类

class MyClass
{

}

 

5.ctor   快速创建构造函数

 public MyClass()
{

}

 

6.cw  快速生成输出打印

 Console.WriteLine();

 

7.do   快速生成循环

 do
{

} while (true);

 

8.else   快速生成else语句块

 else
{

}

 

9.enum   快速生成枚举

 enum MyEnum
{

}

 

10.equals   快速生成比较

public override bool Equals(object obj)
{
   //       
   // See the full list of guidelines at
   //   http://go.microsoft.com/fwlink/?LinkID=85237  
   // and also the guidance for operator== at
   //   http://go.microsoft.com/fwlink/?LinkId=85238
   //

   if (obj == null || GetType() != obj.GetType())
   {
       return false;
   }

            // TODO: write your implementation of Equals() here
   throw new NotImplementedException();
   return base.Equals(obj);
}

        // override object.GetHashCode
public override int GetHashCode()
{
   // TODO: write your implementation of GetHashCode() here
   throw new NotImplementedException();
   return base.GetHashCode();
}

 

posted @ 2023-09-14 11:04  真真真甜  阅读(67)  评论(0)    收藏  举报