C#常用关键字举例

ops/2024/10/20 8:43:19/

        关键字是 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.ppmy.cn/ops/56053.html

相关文章

Linux服务器集群搭建

Linux服务器搭建 配置网络和主机名 查看虚拟机虚拟网卡ip信息 在NAT设置中查看网关地址 具体的ip根据网关网段设置 设置root账户密码&#xff0c;越简单越好 修改网卡信息 修改网卡配置&#xff0c;改成静态ip的方式 修改ip为静态方式 修改过后重启网卡服务 关闭防火墙…

Android 注解的语法原理和使用方法

Android 注解的语法原理和使用方法 关于我 在 Android 开发中&#xff0c;注解&#xff08;Annotation&#xff09;是一种强大的工具&#xff0c;用于在代码中添加元数据。注解可以简化代码、提高可读性、减少样板代码&#xff0c;并且在一定程度上增强编译时的类型检查。本文…

Linux上将图片转换为PDF

在Linux系统中&#xff0c;将图片转换为PDF文件的常见方法是使用ImageMagick这个工具。 1、下载ImageMagick&#xff1a; 首先需要安装ImageMagick&#xff0c;可以通过包管理器安装&#xff0c;例如在Ubuntu上使用&#xff1a; sudo apt update sudo apt install imagemagic…

白帽工具箱:DVWA中的命令注入(Command Injection)解析与防护策略

&#x1f31f;&#x1f30c; 欢迎来到知识与创意的殿堂 — 远见阁小民的世界&#xff01;&#x1f680; &#x1f31f;&#x1f9ed; 在这里&#xff0c;我们一起探索技术的奥秘&#xff0c;一起在知识的海洋中遨游。 &#x1f31f;&#x1f9ed; 在这里&#xff0c;每个错误都…

Java基础:爬虫

1.本地爬虫 Pattern:表示正则表达式 Matcher:文本匹配器&#xff0c;作用按照正则表达式的规则去读取字符串&#xff0c;从头开始读取。在大串中去找符合匹配规则的子串。 1.2.获取Pattern对象 通过Pattern p Pattern.compile("正则表达式");获得 1.3.…

如何实现一套键盘鼠标控制两台计算机(Mouse Without Borders快速上手教程)

需求背景 当我们需要同时使用一台主机和一台笔记本的时候&#xff0c;如果使用两套键盘和鼠标分别操作各自的系统&#xff0c;非常地不便捷且非常占据桌面空间。那么如何使用一套键盘鼠标控制两台电脑呢&#xff1f; 需求实现 软件说明 我们可以使用微软官方的一款软件Mous…

QT slots 函数

文章目录 概述小结 概述 在Qt中&#xff0c;slots 是一种特殊的成员函数&#xff0c;它们可以与对象发出的信号连接。当信号被触发时&#xff0c;连接的槽函数会被调用。 来个简单的示例吧&#xff0c;如下图&#xff1a; #include <QObject> #include <QDebug>…

c#中的超时终止

在C#中&#xff0c;可以使用CancellationToken和Task的超时机制来实现调用方法时的超时终止。 一 用Task.Delay(int)模拟耗时操作 static async Task Main(string[] args){using (var cts new CancellationTokenSource(1 * 1000)){await doSomething(cts.Token);}Console.Wr…