diff --git a/_config.butterfly.yml b/_config.butterfly.yml index 746f805..9f69263 100644 --- a/_config.butterfly.yml +++ b/_config.butterfly.yml @@ -297,7 +297,7 @@ footer: 归档 | 标签 | 分类
  • - 说说 + 说说 | 网站监控
  • diff --git a/source/_posts/c-first-in-first.md b/source/_posts/c-first-in-first.md index abc03df..3dbdf34 100644 --- a/source/_posts/c-first-in-first.md +++ b/source/_posts/c-first-in-first.md @@ -2,7 +2,9 @@ title: C# 入门 categories: 学习 tags: C# +series: C# abbrlink: bc56f789 +summary: 这篇文章指导初学者在Windows上安装C#开发环境:先下载Visual Studio社区版安装器并勾选“.NET桌面开发”工作负载,可自定义安装路径;随后通过HelloWorld演示C#基础结构,包含命名空间、类、Main入口及Console输出,帮助读者完成首个程序并熟悉默认模板。 date: 2025-08-15 09:56:17 --- 今天开始学习C# diff --git a/source/_posts/cs-2.md b/source/_posts/cs-2.md new file mode 100644 index 0000000..a1bbc8a --- /dev/null +++ b/source/_posts/cs-2.md @@ -0,0 +1,181 @@ +--- +title: C# 基本语法 +categories: 学习 +series: C# +tags: C# +abbrlink: e9a8e898 +summary: 这篇文章通过Rectangle示例讲解C#面向对象基础,展示类与对象如何封装数据及行为,并输出面积15.75。随后介绍using引入命名空间、class声明类、注释、成员变量与函数、实例化流程及标识符规则。重点解析C#9顶级语句,无需Main方法即可在文件顶层直接写代码,编译器自动生成入口,适合脚本化开发,示例演示变量、方法、LINQ与异常处理,简化小型项目结构。 +date: 2025-08-16 18:01:10 +--- +# 简介 +C# 是一种面向对象的编程语言。在面向对象的程序设计方法中,程序由各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是在相同的 class 中。 + +例如,以 Rectangle(矩形)对象为例。它具有 length 和 width 属性。根据设计,它可能需要接受这些属性值、计算面积和显示细节。 + +实例 +```c# +using System; +namespace RectangleApplication +{ + 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(); + } + } +} +``` +尝试一下 » +当上面的代码被编译和执行时,它会产生下列结果: +``` +Length: 4.5 +Width: 3.5 +Area: 15.75 +``` +# using 关键字 +在任何 C# 程序中的第一条语句都是: + +using System; +using 关键字用于在程序中包含命名空间。一个程序可以包含多个 using 语句。 + +# class 关键字 +class 关键字用于声明一个类。 + +# C# 中的注释 +注释是用于解释代码。编译器会忽略注释的条目。在 C# 程序中,多行注释以 /* 开始,并以字符 */ 终止,如下所示: +```c# +/* 这个程序演示 +C# 的注释 +使用 */ +``` +单行注释是用 // 符号表示。例如: +```c# +// 这一行是注释 +``` +# 成员变量 +变量是类的属性或数据成员,用于存储数据。在上面的程序中,Rectangle 类有两个成员变量,名为 length 和 width。 + +成员函数 +函数是一系列执行指定任务的语句。类的成员函数是在类内声明的。我们举例的类 Rectangle 包含了三个成员函数: AcceptDetails、GetArea 和 Display。 + +实例化一个类 +在上面的程序中,类 ExecuteRectangle 是一个包含 Main() 方法和实例化 Rectangle 类的类。 + +# 标识符 +标识符是用来识别类、变量、函数或任何其它用户定义的项目。在 C# 中,类的命名必须遵循如下基本规则: + +* 标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下划线( _ )、@。 +* 标识符中的第一个字符不能是数字。 +* 标识符必须不包含任何嵌入的空格或符号,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \。 +* 标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字。 +* 标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。 +* 不能与C#的类库名称相同。 +# C# 关键字 +关键字是 C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。 + +在 C# 中,有些关键字在代码的上下文中有特殊的意义,如 get 和 set,这些被称为上下文关键字(contextual keywords)。 + +# 顶级语句(Top-Level Statements) +在 C# 9.0 版本中,引入了顶级语句(Top-Level Statements)的概念,这是一种新的编程范式,允许开发者在文件的顶层直接编写语句,而不需要将它们封装在方法或类中。 + +## 特点: + +无需类或方法:顶级语句允许你直接在文件的顶层编写代码,无需定义类或方法。 + +文件作为入口点:包含顶级语句的文件被视为程序的入口点,类似于 C# 之前的 Main 方法。 + +自动 Main 方法:编译器会自动生成一个 Main 方法,并将顶级语句作为 Main 方法的主体。 + +支持局部函数:尽管不需要定义类,但顶级语句的文件中仍然可以定义局部函数。 + +更好的可读性:对于简单的脚本或工具,顶级语句提供了更好的可读性和简洁性。 + +适用于小型项目:顶级语句非常适合小型项目或脚本,可以快速编写和运行代码。 + +与现有代码兼容:顶级语句可以与现有的 C# 代码库一起使用,不会影响现有代码。 + +传统 C# 代码 - 在使用顶级语句之前,你必须像这样编写一个 C# 程序: + +实例 +```c# +using System; + +namespace MyApp +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} +``` +使用顶级语句的 C# 代码 - 使用顶级语句,可以简化为: + +实例 +```c# +using System; + +Console.WriteLine("Hello, World!"); +``` +顶级语句支持所有常见的 C# 语法,包括声明变量、定义方法、处理异常等。 + +实例 +```c# +using System; +using System.Linq; + +// 顶级语句中的变量声明 +int number = 42; +string message = "The answer to life, the universe, and everything is"; + +// 输出变量 +Console.WriteLine($"{message} {number}."); + +// 定义和调用方法 +int Add(int a, int b) => a + b; +Console.WriteLine($"Sum of 1 and 2 is {Add(1, 2)}."); + +// 使用 LINQ +var numbers = new[] { 1, 2, 3, 4, 5 }; +var evens = numbers.Where(n => n % 2 == 0).ToArray(); +Console.WriteLine("Even numbers: " + string.Join(", ", evens)); + +// 异常处理 +try +{ + int zero = 0; + int result = number / zero; +} +catch (DivideByZeroException ex) +{ + Console.WriteLine("Error: " + ex.Message); +} +``` \ No newline at end of file diff --git a/source/_posts/custom-footer.md b/source/_posts/custom-footer.md index c99b581..f082c2d 100644 --- a/source/_posts/custom-footer.md +++ b/source/_posts/custom-footer.md @@ -2,9 +2,9 @@ title: 自定义页脚(新) abbrlink: c6143ad3 date: 2025-08-14 17:05:27 -tags: -category: -series: +tags: 网站 +category: 建站手札 +series: webcustom --- # 添加CSS {% note warning 1 %}