二、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 | |

相关推荐
喜欢吃燃面4 分钟前
C++算法竞赛:位运算
开发语言·c++·学习·算法
草莓熊Lotso7 分钟前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
开发语言·c++·经验分享·笔记·其他
谱写秋天14 分钟前
Qt 5.5 的安装与配置(使用 VSCode编辑)
开发语言·vscode·qt
唐青枫15 分钟前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
项目申报小狂人15 分钟前
算法应用上新!自适应更新策略差分进化算法求解球形多飞行器路径规划问题,附完整MATLAB代码
开发语言·算法·matlab
阿珊和她的猫4 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234177 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~7 小时前
C#---StopWatch类
开发语言·c#
lifallen9 小时前
Java Stream sort算子实现:SortedOps
java·开发语言