首页 > 编程语言 >C#常用关键字举例

C#常用关键字举例

时间:2024-07-04 09:29:30浏览次数:21  
标签:Console C# void class 关键字 int 举例 MyClass public

        关键字是 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 operation
        return 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; }
        }
    }
    

标签:Console,C#,void,class,关键字,int,举例,MyClass,public
From: https://blog.csdn.net/sunyao1314/article/details/140141720

相关文章

  • 只需7个小时,利用ChatGPT撰写学术论文的最详细实操指南
    欢迎关注,为大家带来最酷最有效的智能AI学术科研写作攻略。关于使用ChatGPT等AI学术科研的相关问题可以和作者七哥yida985交流地表功能最强大的高级学术AI专业版已经开放,拥有全球领先的GPT学术科研应用,有兴趣的朋友可以联系获取试用,联系七哥获取学术AI使用教程。以下是一系列......
  • 前端面试题(CSS篇一)
    一、介绍一下标准的CSS的盒子模型?低版本IE的盒子模型有什么不同的?相关知识点:(1)有两种盒模型:IE盒模型(border-box)、W3C标准盒模型(content-box)(2)盒模型:分为内容(content)、填充(padding)、边界(margin)、边框(border)四个部分IE盒模型和W3C标准盒模型的区别:(1)W3C标准盒模型......
  • 修复Scikit-learn中的`ValueError: Input contains NaN`
    @TOC博主默语带您GotoNewWorld.✍个人主页——默语的博客......
  • IDEA连接docker
    修改配置文件vi/usr/lib/systemd/system/docker.service修改ExecStart为ExecStart=/usr/bin/dockerd-Hfd://--containerd=/run/containerd/containerd.sock--tlsverify--tlscacert=/etc/ docker/certs/ca.pem--tlscert=/etc/docker/certs/server-cert.pem--tlskey=/......
  • Oracle 临时表 OracleDataAdapter 批量更新
    ///<summary>///注意,字段名必需大小写保持一致///</summary>protectedstaticstringupdateSql=@"MergeintoTable_NameTUsingTempTableSON(T.USER_ID=S.USERID)WHENMATCHEDTHENUPDATESETT.NICK_NAME=S......
  • C/C++ 比较运算的注意事项
    C/C++中比较运算是一个基础且重要的概念,它用于比较两个或多个表达式的值,并返回一个布尔结果(在C/C++中以整数形式表示,真为1,假为0)。以下是一些关于C/C++比较运算的注意事项:1.比较运算符C/C++中的比较运算符包括:==(等于)!=(不等于)<(小于)>(大于)<=(小于等于)>=(大于等于)这......
  • P7690 [CEOI2002] A decorative fence 题解
    题目传送门前置知识计数DP解法方案数统计同luoguP2467[SDOI2010]地精部落,但部分写得不太好看的状态转移方程在本题中并不适用,但仍可借鉴其“离散化”思想。考虑试填。设\(f_{i,j,0/1}\)表示用\(i\)块不同的木板构成栅栏,其中最左边的木板的长度从小到大排在第\(j\)......
  • linux安装netcore nginx postgresql ssh
    sudosu1.安装软件$sudoaptinstall软件名2.卸载软件$sudoaptremove软件名3.更新可用软件包列表$sudoaptupdate4.更新已安装的包$sudoaptupgrade通常安装完ubuntu之后,可以先使用upgrade更新一下当前系统中可以升级的的软件包$sudoaptupdate$sudoap......
  • 编译elementUI主题scss
    elementVue2工程1. 安装包"gulp":"^4.0.2","gulp-autoprefixer":"^8.0.0","gulp-minify-css":"^1.2.4","gulp-sass":"^4.0.2","gulp-uglify":"^3.0.2",2.......
  • OpenStack Yoga版安装笔记(四)keystone练习
    1、keyston安装过程在安装过程中,首先需要在controllernode上的MariaDB中创建一个名为keystone的数据库。接着,在controllernode上安装Keystone软件包,并配置数据库连接。Keystone和数据库可以部署在不同的服务器上,Keystone通过解析主机名“controller”来访问数据库。2、......