当前位置: 首页 > news >正文

C#常用关键字举例

        关键字是 C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。

  1. class:

    public class MyClass
    {// Class definition
    }
    
  2. interface:

    public interface IMyInterface
    {void MyMethod();
    }
    
  3. public:

    public class MyClass
    {public int MyProperty { get; set; }
    }
    
  4. private:

    public class MyClass
    {private int myPrivateField;
    }
    
  5. protected:

    public class MyBaseClass
    {protected int MyProtectedField;
    }
    
  6. internal:

    internal class MyInternalClass
    {// Internal class accessible within the same assembly
    }
    
  7. static:

    public static class MyStaticClass
    {public static void MyStaticMethod(){// Static method}
    }
    
  8. void:

    public class MyClass
    {public void MyMethod(){// Method that returns void}
    }
    
  9. new:

    public class MyBaseClass
    {public virtual void MyMethod(){// Base method}
    }public class MyDerivedClass : MyBaseClass
    {public new void MyMethod(){// Hides base class method with new implementation}
    }
    
  10. virtualoverride:

    public class MyBaseClass
    {public virtual void MyMethod(){// Virtual method}
    }public class MyDerivedClass : MyBaseClass
    {public override void MyMethod(){// Overrides base class method}
    }
    
  11. abstract:

    public abstract class MyAbstractClass
    {public abstract void MyAbstractMethod();
    }
    
  12. this:

    public class MyClass
    {private int value;public void SetValue(int value){this.value = value; // 'this' refers to the current instance of MyClass}
    }
    
  13. base:

    public class MyBaseClass
    {protected int baseValue;public MyBaseClass(int value){baseValue = value;}
    }public class MyDerivedClass : MyBaseClass
    {public MyDerivedClass(int derivedValue) : base(derivedValue){// Calls base class constructor with 'derivedValue'}
    }
    
  14. readonlyconst:

    public class MyClass
    {public const int MyConstant = 10;public readonly int MyReadOnlyField;public MyClass(int value){MyReadOnlyField = value; // Readonly field can be initialized in constructor}
    }
    
  15. delegate:

    public delegate void MyDelegate(string message);public class MyClass
    {public void MyMethod(string message){Console.WriteLine(message);}
    }// Usage of delegate:
    // MyDelegate handler = new MyDelegate(new MyClass().MyMethod);
    
  16. event:

    public class MyClass
    {public event EventHandler MyEvent;public void RaiseEvent(){MyEvent?.Invoke(this, EventArgs.Empty);}
    }
    
  17. trycatchfinally:

    try
    {// Code that may throw exceptions
    }
    catch (Exception ex)
    {// Handle exceptions
    }
    finally
    {// Code that always runs, whether an exception occurred or not
    }
    
  18. ifelseswitchcase:

    int x = 10;if (x > 5)
    {Console.WriteLine("x is greater than 5");
    }
    else
    {Console.WriteLine("x is less than or equal to 5");
    }switch (x)
    {case 5:Console.WriteLine("x is 5");break;default:Console.WriteLine("x is not 5");break;
    }
    
  19. forwhiledo:

    for (int i = 0; i < 5; i++)
    {Console.WriteLine(i);
    }int j = 0;
    while (j < 5)
    {Console.WriteLine(j);j++;
    }int k = 0;
    do
    {Console.WriteLine(k);k++;
    } while (k < 5);
    
  20. foreach:

    int[] numbers = { 1, 2, 3, 4, 5 };foreach (int num in numbers)
    {Console.WriteLine(num);
    }
    
  21. lock:

    private object lockObject = new object();public void AccessSharedResource()
    {lock (lockObject){// Code inside this block is thread-safe}
    }
    
  22. using:

    using (var resource = new DisposableResource())
    {// Use 'resource' here
    }
    
  23. asyncawait:

    public async Task<int> GetValueAsync()
    {await Task.Delay(1000); // Simulates an async operationreturn 10;
    }// Example of usage:
    // int result = await GetValueAsync();
    
  24. getset:

    public class MyClass
    {private int myProperty;public int MyProperty{get { return myProperty; }set { myProperty = value; }}
    }
    
http://www.lryc.cn/news/394640.html

相关文章:

  • stm32——外部中断EXTI
  • Solidity:变量数据存储和作用域 storage/memory/calldata
  • ElementUI中的el-table解决宽度问题 - 根据内容自动撑开
  • react apollo hooks
  • Android 10.0 SystemUI启动流程
  • 洛谷 P1032 [NOIP2002 提高组] 字串变换
  • 网络资源模板--Android Studio 外卖点餐App
  • 【Linux】网络新手村
  • 123123
  • 在pycharm中使用jupyter
  • MongoDB:掌握核心常用命令语句,精通数据操作
  • Redis中测试Stream的例子
  • 28 H3C SecPath F1000 概览(主要功能是总 观看全局)
  • 标准版视频检测终端功能有哪些? 捷顺高清视频车位引导系统怎么样?
  • 说明本文档目录是软件开发梳理需求常见问题QA文档,方便客户看,也方便我们的售前人员,需求分析人员,ui设计师,原型绘图人员,思维导图绘图人员查看。
  • Echarts桑基图
  • wordpress网站添加一个临时维护功能
  • 充电桩开源平台,开发流程有图有工具
  • 数据中台设计书及建设指南(中台及大数据解决技术方案)
  • 合合信息大模型“加速器”重磅上线
  • # Sharding-JDBC 从入门到精通(10)- 综合案例(三)查询商品与测试及统计商品和总结
  • ASRock Creator系列GPU:为AI推理及多GPU系统打造,采用16针电源接口的Radeon RX 7900系列显卡
  • AntV X6 图编辑引擎速通
  • 【若依前后端分离】通过输入用户编号自动带出部门名称(部门树)
  • AIGC时代程序员的跃迁——编程高手的密码武器
  • 园区智慧能源可视化:智能监控与优化能源管理
  • Linux内网端口转公网端口映射
  • 银河麒麟高级服务器操作系统(通用)安装和编译指定的python3版本
  • cs231n 作业3
  • HarmonyOS Next系列之Echarts图表组件(折线图、柱状图、饼图等)实现(八)