二、C#基本语法

C#是一种面向对象的编程语言。在面向对象的程序设计方法中,程序由各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是相同的class中。

例如,以rectangle(矩形)对象为例。它具有length和width属性。根据设计,它可能需要接受这些属性值,计算面积和显示细节。

cs 复制代码
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
namespace RectanglApplication
{
    class Rectangle
    {
    
    //成员变量
    double length;
    double width;
    public void Acceptdetails()
    {
        length = 4.5;
        width = 3.5;
        
    }
    public double GetArea()
    {
        return length  *  width;
    }
    public void Display()
    {
        Console.WriteLine("length :{0}", length);
        Console.WriteLine("width :{0}", width);
        Console.WriteLine("Area:{0}", GetArea());
    }
}
class ExecuteRectangle
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        r.Acceptdetails();
        r.Display();
        Console.ReadLine();
    }
}
}

运行结果如下:

C# 关键字

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

在 C# 中,有些关键字在代码的上下文中有特殊的意义,如 get 和 set,这些被称为上下文关键字(contextual keywords)。

下表列出了 C# 中的保留关键字(Reserved Keywords)和上下文关键字(Contextual Keywords):

|------------------|-----------|-----------|------------|------------------------|-----------------------|----------------|
| 保留关键字 |||||||
| abstract | as | base | bool | break | byte | case |
| catch | char | checked | class | const | continue | decimal |
| default | delegate | do | double | else | enum | event |
| explicit | extern | false | finally | fixed | float | for |
| foreach | goto | if | implicit | in | in (generic modifier) | int |
| interface | internal | is | lock | long | namespace | new |
| null | object | operator | out | out (generic modifier) | override | params |
| private | protected | public | readonly | ref | return | sbyte |
| sealed | short | sizeof | stackalloc | static | string | struct |
| switch | this | throw | true | try | typeof | uint |
| ulong | unchecked | unsafe | ushort | using | virtual | void |
| volatile | while | | | | | |
| 上下文关键字 |||||||
| add | alias | ascending | descending | dynamic | from | get |
| global | group | into | join | let | orderby | partial (type) |
| partial (method) | remove | select | set | |

相关推荐
常利兵10 小时前
Kotlin抽象类与接口:相爱相杀的编程“CP”
android·开发语言·kotlin
2501_9444484710 小时前
数据可视化 Kotlin KMP OpenHarmony图表生成
开发语言·信息可视化·harmonyos
Arkerman_Liwei10 小时前
Android 新开发模式深度实践:Kotlin + 协程 + Flow+MVVM
android·开发语言·kotlin
xinhuanjieyi10 小时前
MCP分析某wordpress网站 时间所在的背景动画,并用php框架webman复刻下来
开发语言·php
jwn99910 小时前
Laravel1.x:PHP框架的初心与革新
开发语言·php
蜡台10 小时前
JavaScript async和awiat 使用
开发语言·前端·javascript·async·await
蹦哒10 小时前
Kotlin DSL 风格编程详解
android·开发语言·kotlin
枫叶丹410 小时前
【HarmonyOS 6.0】ArkWeb 深度解读:getPageOffset20 与网页滚动偏移量获取能力的演进
开发语言·华为·harmonyos
独特的螺狮粉10 小时前
开源鸿蒙跨平台Flutter开发:室内探险游戏应用
开发语言·flutter·游戏·华为·开源·harmonyos·鸿蒙
坏小虎10 小时前
~/.zshrc 和 ~/.bash_profile 详细介绍与区别
开发语言·bash