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

相关推荐
pumpkin845141 小时前
Rust Mock 工具
开发语言·rust
love530love1 小时前
【笔记】在 MSYS2(MINGW64)中安装 python-maturin 的记录
运维·开发语言·人工智能·windows·笔记·python
阿卡蒂奥2 小时前
C# 结合PaddleOCRSharp搭建Http网络服务
开发语言·http·c#
江沉晚呤时4 小时前
SQL Server 事务详解:概念、特性、隔离级别与实践
java·数据库·oracle·c#·.netcore
泉飒4 小时前
lua注意事项
开发语言·笔记·lua
hao_wujing4 小时前
使用逆强化学习对网络攻击者的行为偏好进行建模
开发语言·网络·php
还是鼠鼠5 小时前
单元测试-概述&入门
java·开发语言·后端·单元测试·log4j·maven
明月看潮生6 小时前
青少年编程与数学 02-020 C#程序设计基础 14课题、程序调试
开发语言·青少年编程·c#·编程与数学
抽风的雨6106 小时前
【python深度学习】Day 42 Grad-CAM与Hook函数
开发语言·python·深度学习
Mikhail_G7 小时前
Python应用for循环临时变量作用域
大数据·运维·开发语言·python·数据分析