Visual Studio常用代码片段整理(二)
1.for 快速生成循环
for (int i = 0; i < length; i++) { }
2.foreach 快速生成遍历
foreach (var item in collection) { }
3.forr 快速生成倒叙循环
for (int i = length - 1; i >= 0; i--) { }
4.#if 快速生成预处理命令(参考:https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/preprocessor-directives)
#if true #endif
如:即表示在DeBug下会编译并执行以下代码
#if DEBUG Console.WriteLine("Debug version"); #endif
其中预处理命令还有#
#define MYTEST using System; public class MyClass { static void Main() { #if (DEBUG && !MYTEST) Console.WriteLine("DEBUG is defined"); #elif (!DEBUG && MYTEST) Console.WriteLine("MYTEST is defined"); #elif (DEBUG && MYTEST) Console.WriteLine("DEBUG and MYTEST are defined"); #else Console.WriteLine("DEBUG and MYTEST are not defined"); #endif } }
5.indexer 快速设置索引
public object this[int index] { get { /* return the specified index here */ } set { /* set the specified index to value here */ } }
6.interface 快速创建接口类
interface IInterface { }
EventHandler temp = MyEvent; if (temp != null) { temp(); }
8.iterator 快速生成迭代器
public System.Collections.Generic.IEnumerator<ElementType> GetEnumerator() { throw new NotImplementedException(); yield return default(ElementType); }
9.lock 快速生成lock代码块
lock (this) { }
10.mbox(仅针对WinForm):快速生成弹框
System.Windows.Forms.MessageBox.Show("Test");
11.namespace 快速生成命名空间
namespace MyNamespace { }
12.prop 快速生成属性
public int MyProperty { get; set; }
13.propfull 快速生成完整属性
private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } }
14.propg 快速生成专用“set”访问器的属性
public int MyProperty { get; private set; }
15.sim 和svm异曲同工,区别只是i=int ;v=void 表示不同的返回类型
static int Main(string[] args) { return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构