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(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构